【问题标题】:Save json string to arraylist将json字符串保存到arraylist
【发布时间】:2014-06-30 07:24:24
【问题描述】:

下面的JSONObject格式字符串存储在arraylist ArrayListlatestNews里面;

response.toString() 是

{
    "blue": [
        {
            "title": "http:\\www.b"
        },
        {
            "info": " http:\\www.b"
        },
        {
            "link": "http:\\www.b"
        },
        {
            "image": "http:\\www.b"
        },
        {
            "pubDate": "http:\\www.b"
        },
        {
            "guid": "http:\\www.b"
        }
    ]
}{
    "blue": [
        {
            "title": "http"
        },
        {
            "info": " 5449 1616"
        },
        {
            "link": "http:\\www.b"
        },
        {
            "image": "http:\\www.b"
        },
        {
            "pubDate": "Thu, 26 Jun 2014 16:25:25 GMT"
        },
        {
            "guid": "http:\\www.b"
        },
        {
            "title": "Weekend"
        },
        {
            "info": " 3345 4353"
        },
        {
            "link": "http:\\www.b.."
        },
        {
            "image": "http:\\www.b..."
        },
        {
            "pubDate": "Mon, 23 Jun 2014 09:47:25 GMT"
        },
        {
            "guid": "http:\\www.b"
        }
    ]
}{
    "blue": [
        {
            "title": "KARAVAN "
        },
        {
            "info": " 4444444"
        },
        {
            "link": "http:\\www...."
        },
        {
            "image": "http:\\www...."
        },
        {
            "pubDate": "Thu, 26 Jun 2014 16:25:25 GMT"
        },
        {
            "guid": "http:\\www...."
        },
        {
            "title": "IMC "
        },
        {
            "info": " 134 3333"
        },
        {
            "link": "http:\\www...."
        },
        {
            "image": "http:\\www...."
        },
        {
            "pubDate": "Mon, 23 Jun 2014 09:47:25 GMT"
        },
        {
            "guid": "http:\\www...."
        },
        {
            "title": "property "
        },
        {
            "info": " 633 1352 \ 537 18211"
        },
        {
            "link": "http:\\www....l"
        },
        {
            "image": "http:\\www...."
        },
        {
            "pubDate": "Thu, 26 Jun 2014 16:09:01 GMT"
        },
        {
            "guid": "http:\\www...."
        }
  ....

LatestNews.class

public class LatestNews {

    public String title;
    public String info;
    public String link;
    public String image;
    public String pubDate;
    public String guid;

    public LatestNews (String title_,String info_,String link_,String image_,String pubDate_,String guid_){

        this.title=title_;
        this.info=info_;
        this.link=link_;
        this.image=image_;
        this.pubDate=pubDate_;
        this.guid=guid_;

    }

}

我正在提取json如下

JSONObject obj = new JSONObject(response.toString());
                 JSONArray venues = obj.getJSONArray("blue");
                System.out.println("response object "+venues.length());

                for (int x=0;x<venues.length();x++){
                    JSONObject keyValue = venues.getJSONObject(x);

                    if (keyValue.has("title")){
                        System.out.println("xxxtitle "+keyValue.getString("title"));

                    }

                }

但是当我打印 keyValue.getString("title") 时,只打印第一个标题。如何打印每个蓝色对象中的所有元素,然后将其保存到数组列表中。欢迎任何帮助。

【问题讨论】:

    标签: android arraylist jsonobject


    【解决方案1】:

    首先,对于大多数情况,您的 JSON 响应不符合标准和优化的响应。

    实现您的 Web 服务逻辑以获取以下格式的响应,这也将帮助您减少解析逻辑/代码和迭代,这也将帮助您提高应用程序性能。

    {
        "blue": [
            {
                "title": "http:\\www.b",
                "info": " http:\\www.b",
                "link": "http:\\www.b",
                "image": "http:\\www.b",
                "pubDate": "http:\\www.b",
                "guid": "http:\\www.b"
            },
            {
                "title": "http:\\www.b",
                "info": " http:\\www.b",
                "link": "http:\\www.b",
                "image": "http:\\www.b",
                "pubDate": "http:\\www.b",
                "guid": "http:\\www.b"
            }
        ]
    }
    

    现在关于 JSON 解析教程/代码,网络上和 Stackoverflow 上也有大量示例,请看:JSON Parsing in Android

    您的代码和 JSON 格式/逻辑中的错误: 正如你所说,只有第一个标题被打印出来,背后的原因是你写了obj.getJSONArray("blue");,它只会给你第一个“蓝色”对象,所以它会给你一个来自蓝色对象的标题值。

    我已经在上面提出了优化方案!

    【讨论】:

    • @blackbelt 不应该,因为我正在更详细地更新答案。我已经知道规则了。不过谢谢:)
    • 我知道您知道规则。无论如何,如果问题是格式错误的 JSON,我认为对象的创建将失败。
    • @blackbelt 上面的答案不可能放在评论中
    • @blackbelt 只需使用 错误 详细信息检查更新的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-21
    • 2012-07-04
    • 2019-08-28
    • 2021-12-12
    • 1970-01-01
    • 2015-11-15
    相关资源
    最近更新 更多