在Android中解析JSON;arrayList_ph为空
请帮助我解析以下给出的JSON数据:在Android中解析JSON;arrayList_ph为空,android,json,Android,Json,请帮助我解析以下给出的JSON数据: { posts: [ { count: 1, user_id: "1", name: "Dave Greeneberg", email: "daveneberg@example.com", profile_photo: "http://phontest.lbch.com//users/user1.jpg", contest_count: "3", photo_co
{
posts: [
{
count: 1,
user_id: "1",
name: "Dave Greeneberg",
email: "daveneberg@example.com",
profile_photo: "http://phontest.lbch.com//users/user1.jpg",
contest_count: "3",
photo_count: 19,
win_count: "0",
photos: [
"images/contest/diwali1.jpg",
"images/contest/diwalc2.jpg",
"images/contest/145043cd811.png",
"images/contest/145043def03411.jpg",
"images/contest/14504ger11.jpg"
]
}
]
}
我尝试了以下代码,但arrayList\u ph
中的值为空。我对如何解析这个JSON内容感到困惑
JSONObject object = new JSONObject(json);
JSONArray arr = object.getJSONArray("posts");
for (int index = 0; index < arr.length(); index++) {
JSONObject object1 = arr.getJSONObject(index);
user = arr.getJSONObject(0).getString("name");
user_email = arr.getJSONObject(0).getString("email");
user_profile = arr.getJSONObject(0).getString("profile_photo");
user_count = arr.getJSONObject(0).getString("count");
user_photo_count = arr.getJSONObject(0).getInt("photo_count");
contest_count = arr.getJSONObject(0).getString("contest_count");
win_count = arr.getJSONObject(0).getString("win_count");
JSONArray ph_arr= arr.getJSONObject(0).getJSONArray("photos");
for (int in = 0; in < ph_arr.length(); in++) {
arrayList_ph.add(ph_arr.getString(in));
}
}
JSONObject object=新的JSONObject(json);
JSONArray arr=object.getJSONArray(“posts”);
对于(int index=0;index
请帮助我分析该字段。请尝试-
JSONObject object = new JSONObject(json);
JSONArray arr = object.getJSONArray("posts”);
for (int index = 0; index < arr.length(); index++) {
JSONObject object1 = arr.getJSONObject(index);
user = object1.getString("name");
user_email = object1.getString("email");
user_profile = object1.getString("profile_photo");
user_count = object1.getInt("count");
user_photo_count = object1.getInt("photo_count");
contest_count = object1.getString("contest_count");
win_count = object1.getString("win_count");
JSONArray photos_arr= object1.getJSONArray("photos");
for (int in = 0; in < ph_arr.length(); in++) {
String str = ph_arr.get(in).toString();
arrayList_ph.add(str);
}
}
JSONObject object=新的JSONObject(json);
JSONArray arr=object.getJSONArray(“posts”);
对于(int index=0;index
将解析代码更改为
JSONObject object = new JSONObject(json);
JSONArray arr = object.getJSONArray("posts");
for (int index = 0; index < arr.length(); index++) {
JSONObject object1 = arr.getJSONObject(index);
user = object1.getString("name");
user_email = object1.getString("email");
user_profile = object1.getString("profile_photo");
user_count = object1.getString("count");
user_photo_count = object1.getInt("photo_count");
contest_count = object1.getString("contest_count");
win_count = object1.getString("win_count");
JSONArray ph_arr= object1.getJSONArray("photos");
for (int in = 0; in < ph_arr.length(); in++) {
String str = ph_arr.get(in).toString();
arrayList_ph.add(str);
}
}
JSONObject object=新的JSONObject(json);
JSONArray arr=object.getJSONArray(“posts”);
对于(int index=0;index
请尝试此解决方案。
这个解决方案对我有用
JSONObject object = new JSONObject("");
JSONArray arr = object.optJSONArray("posts");
for (int index = 0; index < arr.length(); index++) {
JSONObject object1 = arr.optJSONObject(index);
user = object1.optString("name");
user_email = object1.optString("email");
user_profile = object1.optString("profile_photo");
user_count = object1.optString("count");
user_photo_count = object1.optInt("photo_count");
contest_count = object1.optString("contest_count");
win_count = object1.optString("win_count");
JSONArray ph_arr = object1.optJSONArray("photos");
for (int in = 0; in < ph_arr.length(); in++) {
String str = ph_arr.opt(in).toString();
arrayList_ph.add(str);
}
}
JSONObject object=newjsonobject(“”);
JSONArray arr=object.optJSONArray(“posts”);
对于(int index=0;index
请交叉核对以下内容:
1) 我认为您的json响应格式不正确
从此处验证:
您的json响应应该是:
{
"posts":[
{
"count":1,
"user_id":"1",
"name":"Dave Greeneberg",
"email":"daveneberg@example.com",
"profile_photo":"http://phontest.lbch.com//users/user1.jpg",
"contest_count":"3",
"photo_count":19,
"win_count":"0",
"photos":[
{
"image":"images/contest/diwali1.jpg"
},
{
"image":"images/contest/diwalc2.jpg"
},
{
"image":"images/contest/145043cd811.png"
},
{
"image":"images/contest/145043def03411.jpg"
},
{
"image":"images/contest/14504ger11.jpg"
}
]
}
]
}
2) 如果仍然不起作用…请尝试调试并发布日志…您已经提出了此类问题,因此不要重复该问题中的同一类型问题。问题不在JSON解析中,JSON内容与上述内容不相同。它正在我的机器上工作。您那边有什么错误?@Krishnaraylist\u ph没有继续n任何值数组列表\u ph是您必须创建的数组,用于存储在类中声明的值@krishnaits在我的类中声明的值我认为您在照片数组中的某个地方出错了。@krishna