如何在android中解析嵌套的JSONArray

时间:2016-12-15 17:17:32

标签: android json android-json

这是我的json:

{
  "posts": [
    {    
      "id": "c200",
      "title": "erfan",
      "email": "erfan@gmail.com",
      "address": "xx-xx-xxxx,x - street, x - country",
      "gender" : "male",
  "attachments":[
  {
    "url":"http://memaraneha.ir/wp-content/uploads/2016/11/light.jpg"
  }]},
  {
    "id": "c201",
    "name": "Johnny Depp",
    "email": "johnny_depp@gmail.com",
    "address": "xx-xx-xxxx,x - street, x - country",
    "gender" : "male",
    "attachments":[
    {
      "url":"http://memaraneha.ir/wp-content/uploads/2016/11/renovate-old-home.jpg"}]
    }]
}

我想在title数组中获取urlattachments。到目前为止我已经尝试过了。

try {
    JSONObject jsonObj = new JSONObject(jsonStr);
    jsonContent jsonContent = new jsonContent(); //my class 

    JSONArray posts = jsonObj.getJSONArray("posts");

    for (int i = 0; i < posts.length(); i++) {
        JSONObject c = posts.getJSONObject(i);
        jsonContent.title = c.getString("title");

        JSONArray attachments=c.getJSONArray("attachments");

        for (int j=0;j<attachments.length();j++) {
            JSONObject a=attachments.getJSONObject(j);                     
            jsonContent.imgurl=a.getString("url");
        }

        listcontent.add(jsonContent);    //mylist
    }
} catch(Exception e) {}

然后,我在title内的urlTextView中显示ImageViewRecyclerView。在卡片1中,我显示了解析的json中的第一个标题和图像,它工作正常。但问题在于卡片2,它显示了card1的标题和图像,并且无论我们拥有多少json数据,它只是在我的RecyclerView的所有项目中显示相同的标题和图像。

这是用于解析Json响应的POJO。

public class jsonContent {
    public String title;
    public String imgurl;
}

2 个答案:

答案 0 :(得分:1)

您需要在每个循环中创建新的jsonContent对象,因为目前您只是在每次迭代中更改同一对象的值并将其添加到列表中。

 jsonContent jsonContent=null; //my class 

    JSONArray posts = jsonObj.getJSONArray("posts");
    for (int i = 0; i < posts.length(); i++) {
        jsonContent=new jsonContent(); //my class 
        //          ^^^^^^^^  create new object   
        JSONObject c = posts.getJSONObject(i);
        jsonContent.title=c.getString("title");

        JSONArray attachments=c.getJSONArray("attachments");
        for (int j=0;j<attachments.length();j++)
        {
            JSONObject a=attachments.getJSONObject(j);                     
            jsonContent.imgurl=a.getString("url");
        }
        listcontent.add(jsonContent);    //mylist
   }

改进:使用getter和setter函数,而不是将类字段公开为public

答案 1 :(得分:0)

我建议使用Gson来解析你的json字符串。它干净,易于使用。

在您的情况下,您可能必须创建这些类。

public class jsonContent {
    public String id;
    public String title;
    public String email;
    public String address;
    public String gender;
    public Attachment[] attachments;
}

public class Attachment {
    public String[] url;
}

现在,当您的POJO被创建时,您现在可以使用Gson来解析您在那里获得的json字符串。

Gson gson = new Gson();
jsonContent[] postArray = gson.fromJson(jsonStr, jsonContent[].class);

现在将此postArray传递给您的适配器并遍历数组,以便在TextView的每个项目的ImageViewRecyclerView中显示正确的数据。

要在Android项目中添加Gson,您只需在build.gradle中添加以下依赖项。

dependencies {
    compile 'com.google.code.gson:gson:2.4'
}
相关问题