我得到通過凌空一個JSON響應,響應如下:JSON解析的Android JSON數組和JSON對象
{
"status":"success",
"message":"Request Successfull",
"messagetitle":"Success",
"show":"false",
"goback":"false",
"companies":[
{
"id":"14",
"category":{
"id":"1",
"name":"test"
},
"subcategory":{
"id":"1",
"name":"test",
"image":"https:\/\/page.com\/page\/uploads\/pagepics\/test\/testpics\/test.jpg"
},
"name2":"Company",
"location":null,
"logo":"https:\/\/page.com\/test\/testp\/testpics\/logo.png",
"picture":"https:\/\/page.com\/test\/",
"facebook":"https:\/\/www.facebook.com\/test",
"instagram":"",
"twitter":"",
"telephone1":"+9611990454",
"telephone2":"+961000000",
"address":"Lebanon",
"longitude":"0",
"latitude":"0",
"website":"www.website.com",
"email":"",
"desc":"asdasdas das das das dasda das das das dsadasdsadasdsad asd asd asd asd sad sa as das dsa asd das a a",
"user":{
"id":"21",
"name":"X Y"
},
"status":"1"
},
{
"id":"4",
"category":{
"id":"1",
"name":"test"
},
"subcategory":{
"id":"1",
"name":"test",
"image":"https:\/\/page.com\/test\/testp\/testpics\/test\/testphoto\/test.jpg"
},
"name2":"Your Company",
"location":null,
"logo":"",
"picture":"https:\/\/page.com\/test\/",
"facebook":"",
"instagram":"",
"twitter":"",
"telephone1":"",
"telephone2":"",
"address":"",
"longitude":"0",
"latitude":"0",
"website":"",
"email":"",
"desc":"",
"user":{
"id":"1",
"name":"X Y"
},
"status":"1"
}
]
}
我試圖讓所有的「形象」和「NAME2」對象響應。這是我的java代碼:
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
link, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// Log.d("Request", response.toString());
try {
jarray1 = response.getJSONArray("subcategory");
for(int i=0;i<jarray1.length();i++)
{
JSONObject object = (JSONObject) jarray1.get(i);
String url = object.getString("image");
String name= object.getString("name2");
Toast.makeText(getBaseContext(),url+"\n"+name,Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
那麼這是給我錯誤的信息,請任何幫助嗎?
請讓我知道你得到的錯誤,因爲Facebook的屬性是錯誤的我的意思是你需要添加「在Facebook之前 –
當我運行它在for循環,它給了我相同的信息重複,它沒有進入對象和提取字符串。任何幫助? –
爲了上帝的愛和聖潔,請格式化代碼 –