【问题标题】:how to parse Nested JSONArray in android如何在android中解析嵌套的JSONArray
【发布时间】:2017-05-01 09:23:32
【问题描述】:

这是我的 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"}]
    }]
}

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

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

然后我在 TextViewImageView 中显示 titleurl RecyclerView。在卡片 1 中,我展示了解析后的 json 中的第一个标题和图像,效果很好。但问题出在卡片 2 中,它显示了卡片 1 的标题和图像,无论我们有多少 json 数据,它只是在我的RecyclerView 的所有项目中显示相同的标题和图像。

这是解析 Json 响应的 POJO。

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

【问题讨论】:

  • 我不知道listcontent 是什么,但除非add 方法复制其参数,否则您需要为每个post 元素创建一个新的jsonContent 对象。您正在为每个帖子添加相同的对象。
  • How to parse JSON in Java的可能重复
  • @PavneetSingh 如果已经回答了类似的问题,请不要回答您有足够的代表标记为重复...是的,这是无数次,例如herehere
  • 我从来没有说过任何关于复制的事情。那是塞尔文。我建议将jsonContent jsonContent = new jsonContent(); 行移到for 循环内。你试过吗?
  • Pavneet “请不要回答”部分只是对您对 Ted 的评论(您已删除)的评论...我并不是说您应该真正删除您的答案...跨度>

标签: android json android-json


【解决方案1】:

我建议使用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 传递给您的适配器并遍历数组以在RecyclerView 的每个项目中显示TextViewImageView 中的正确数据。

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

dependencies {
    compile 'com.google.code.gson:gson:2.4'
}

【讨论】:

    【解决方案2】:

    您需要在每个循环中创建新的 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 函数,而不是将您的类字段公开

    【讨论】:

    • 这并不能解释 OP 报告的行为。我同意每个循环可能需要一个新的jsonContent,但如果这确实是问题所在,那么列表将填充读取的 last 值,而 OP 报告只有第一个值正在显示所有帖子。
    • @TedHopp 可能是 OP 指示通常第一个值和所有值都相同,因此这是数据的唯一问题并清楚地表明行为
    • @PavneetSingh 只需添加这一行 jsonContent=new jsonContent();在 for 循环和问题解决中。我接受你的回答请投票我的问题 selvin 给我反对票:|
    猜你喜欢
    • 2023-04-08
    • 2015-09-12
    • 2015-02-22
    • 1970-01-01
    • 2017-09-22
    • 1970-01-01
    • 2013-06-12
    • 2017-09-02
    • 1970-01-01
    相关资源
    最近更新 更多