在Android中解析JSON; arrayList_ph为null

时间:2016-01-08 08:02:46

标签: 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_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_ph中的值为null。我对如何解析这个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));
    }

}

请帮我解析那个字段。

4 个答案:

答案 0 :(得分:1)

请尝试此解决方案。 这个解决方案对我有用。

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);
    }
}

答案 1 :(得分:0)

尝试 -

 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);
              }
    }

答案 2 :(得分:0)

将您的解析代码更改为

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);
    }
}

答案 3 :(得分:0)

请交叉检查以下:

1)我认为您的json响应格式不正确。

通过以下方式验证: https://jsonformatter.curiousconcept.com/

你的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)如果仍然无效...请尝试调试并发布您的日志...