我得到通过凌空一个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循环,它给了我相同的信息重复,它没有进入对象和提取字符串。任何帮助? –
为了上帝的爱和圣洁,请格式化代码 –