我有一个json链接,如果我们打开它,我会得到以下结果
{
"Status": "Success",
"All_Details": [{
"Types": "0",
"TotalPoints": "0",
"ExpiringToday": 0
}],
"First": [{
"id": "0",
"ImagePath": "http://first.example.png"
}],
"Second": [{
"id": "2",
"ImagePath": "http://second.example.png"
}],
"Third": [{
"id": "3",
"ImagePath": "http://third.example.png"
}],
Run Code Online (Sandbox Code Playgroud)
}
我需要的是,我想动态获取所有关键名称,如status,All_details,First等.
我还想在All_details和First Array中获取数据.我使用以下方法
@Override
public void onResponse(JSONObject response) throws JSONException {
VolleyLog.d(TAG, "Home Central OnResponse: " + response);
String statusStr = response.getString("Status");
Log.d(TAG, "Status: " + statusStr);
if (statusStr.equalsIgnoreCase("Success")) {
Iterator iterator = response.keys();
while (iterator.hasNext()) {
String key = (String)iterator.next();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到了存储在String键中的所有键名.但是我无法打开获取JSON数组中的值,例如.我需要使用String(Key)获取第一个和第二个数组中的值.我怎样才能做到这一点.???
我有以下 JSON 数据:
[
{
"sendingReqDataSet": {
"totalCount": 1,
"limit": 50,
"offset": 0,
"status_code": true,
"contacts": [
{
"id": 1,
"request_to": "Shehla_kiran",
"request_by": "ayesha",
"product_name": "Dead Space",
"schedule_date_time": "2016-09-21 00:00:00",
"place": "lhr",
"request_type": "Swap",
"status": "Pending"
},
{
"id": 2,
"request_to": "muqadas",
"request_by": "muqadas",
"product_name": "battleField",
"schedule_date_time": "2016-09-22 00:00:00",
"place": "lhr",
"request_type": "Swap",
"status": "Pending"
},
{
"id": 3,
"request_to": "Shehla_kiran",
"request_by": "Shehla_kiran",
"product_name": "Mario",
"schedule_date_time": "2016-09-12 00:00:00",
"place": "Lahore",
"request_type": "Swap",
"status": "Pending"
},
{
"id": 4,
"request_to": "Shehla_kiran", …Run Code Online (Sandbox Code Playgroud) JsonParse.Java试图从URL获取json数组对象,但它抛出了Parsing ERROR,
而CountriedBean是我的POJO类.
parse_json只有ListView,而parse_json_view有4个TextView
任何人都可以帮助我................
private List<CountriesBean> cntries= new ArrayList<CountriesBean>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.parse_json);
RequestQueue queues = Volley.newRequestQueue(JsonParse.this);
JsonObjectRequest myRequest = new JsonObjectRequest(Request.Method.GET,
"https://github.com/SaiNitesh/REST-Web-services/blob/master/RESTfulWS/json_file.json",
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try{
Log.i("myCountries**", "response:" +response);
JSONArray countryItems = response.getJSONArray("countryItems");
Log.i("myTagx", "response:" +countryItems);
for(int i=0; i<countryItems.length();i++){
JSONObject temp= countryItems.getJSONObject(i);
String nm = temp.getString("nm");
String cty = temp.getString("cty");
String hse = temp.getString("hse");
String yrs = temp.getString("yrs");
cntries.add(new CountriesBean(nm, cty, hse, yrs));
}
} catch(JSONException e){ …Run Code Online (Sandbox Code Playgroud) 我想"result"从下面的JSON响应中获取值并将其存储在本地.这是代码:
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
//JSONArray contacts;
contacts = jsonObj.getJSONArray("response");
Log.d("Response: ", "> " + contacts);
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data …Run Code Online (Sandbox Code Playgroud)