首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓JSONException对mediaGroup没有价值

安卓JSONException对mediaGroup没有价值
EN

Stack Overflow用户
提问于 2016-05-28 12:13:35
回答 2查看 182关注 0票数 0

我正在尝试访问我的JSON结果的下一个级别。但是,它一直说mediaGroup元素没有值。我只设法在相同级别上处理其他元素,如title、contentSnippet和publishedDate。

这是json结果的一部分。

代码语言:javascript
复制
{
"responseData": {
    "feed": {
        "feedUrl": "http://www.abc.net.au/news/feed/51120/rss.xml",
        "title": "Just In",
        "link": "http://www.abc.net.au/news/justin/",
        "author": "",
        "description": "",
        "type": "rss20",
        "entries": [
            {
                "mediaGroups": [
                    {
                        "contents": [
                            {
                                "url": "http://www.abc.net.au/news/image/6951914-16x9-2150x1210.jpg",
                                "type": "image/jpeg",
                                "medium": "image",
                                "height": 1210,
                                "width": 2150,
                                "lang": "en-US",
                                "description": "The stockpile at Longford contains more than a million tyres. (ABC News: Damian McIntyre)",
                                "thumbnails": [
                                    {
                                        "height": 105,
                                        "width": 140,
                                        "url": "http://www.abc.net.au/news/image/6951914-4x3-140x105.jpg"
                                    }
                                ]
                            },
                            {
                                "url": "http://www.abc.net.au/news/image/6951914-4x3-940x705.jpg",
                                "type": "image/jpeg",
                                "medium": "image",
                                "height": 705,
                                "width": 940,
                                "lang": "en-US",
                                "description": "The stockpile at Longford contains more than a million tyres. (ABC News: Damian McIntyre)",
                                "thumbnails": [
                                    {
                                        "height": 105,
                                        "width": 140,
                                        "url": "http://www.abc.net.au/news/image/6951914-4x3-140x105.jpg"
                                    }
                                ]
                            },
                            {
                                "url": "http://www.abc.net.au/news/image/6951914-3x2-940x627.jpg",
                                "type": "image/jpeg",
                                "medium": "image",
                                "isDefault": "true",
                                "height": 627,
                                "width": 940,
                                "lang": "en-US",
                                "description": "The stockpile at Longford contains more than a million tyres. (ABC News: Damian McIntyre)",
                                "thumbnails": [
                                    {
                                        "height": 105,
                                        "width": 140,
                                        "url": "http://www.abc.net.au/news/image/6951914-4x3-140x105.jpg"
                                    }
                                ]
                            },
                            {
                                "url": "http://www.abc.net.au/news/image/6951914-3x4-940x1253.jpg",
                                "type": "image/jpeg",
                                "medium": "image",
                                "height": 1253,
                                "width": 940,
                                "lang": "en-US",
                                "description": "The stockpile at Longford contains more than a million tyres. (ABC News: Damian McIntyre)",
                                "thumbnails": [
                                    {
                                        "height": 105,
                                        "width": 140,
                                        "url": "http://www.abc.net.au/news/image/6951914-4x3-140x105.jpg"
                                    }
                                ]
                            },
                            {
                                "url": "http://www.abc.net.au/news/image/6951914-1x1-1400x1400.jpg",
                                "type": "image/jpeg",
                                "medium": "image",
                                "height": 1400,
                                "width": 1400,
                                "lang": "en-US",
                                "description": "The stockpile at Longford contains more than a million tyres. (ABC News: Damian McIntyre)",
                                "thumbnails": [
                                    {
                                        "height": 105,
                                        "width": 140,
                                        "url": "http://www.abc.net.au/news/image/6951914-4x3-140x105.jpg"
                                    }
                                ]
                            }
                        ]
                    }
                ],
                "title": "New proposed tyre shredding facility receives EPA approval",
                "link": "http://www.abc.net.au/news/2016-05-28/tyre-shredding-facility-proposed-tasmania-south-epa-approval/7456326",
                "author": "",
                "publishedDate": "Fri, 27 May 2016 23:42:46 -0700",
                "contentSnippet": "A long-term solution to Tasmania's tyre waste problem is on the horizon, with a new facility preparing to shred thousands of ...",
                "content": "<p>A long-term solution to Tasmania's tyre waste problem is on the horizon, with a new facility preparing to shred thousands of tyres every year.</p>",
                "categories": [
                    "Environmental Impact",
                    "Environment",
                    "Government and Politics"
                ]

这是我的代码来解析它

代码语言:javascript
复制
public List<NewsObj> constructJSON(String jsonIN){
    ArrayList<NewsObj> newsList = new ArrayList<>();
    try{
        //add more levels to extract json
        JSONObject jsonObject1 = new JSONObject(jsonIN);
        String responseData = jsonObject1.getString("responseData");
        Log.d("RECEIVEJSONOBJECTLEVEL1",responseData);

        JSONObject jsonObject2 = new JSONObject(responseData);
        String feed = jsonObject2.getString("feed");
        Log.d("RECEIVEJSONOBJECTLEVEL2",feed);

        JSONObject jsonObject3 = new JSONObject(feed);
        String entries = jsonObject3.getString("entries");
        Log.d("RECEIVEJSONOBJECTLEVEL3",entries); //this opens up further thumbnail sizes



        JSONArray jsonArray1 = new JSONArray(entries);
        for(int i=0; i<jsonArray1.length();i++){
            JSONObject mediaGroups = jsonArray1.getJSONObject(i);
            JSONArray jsonArray2 = mediaGroups.getJSONArray("mediaGroups");
            String title = mediaGroups.getString("title");
            String url = mediaGroups.getString("link");
            String description = mediaGroups.getString("contentSnippet");
            String publishedDate = mediaGroups.getString("publishedDate");
//                main information for news article
            Log.d("RECEIVEJSONOBJECTLEVEL4",title);
            Log.d("RECEIVEJSONOBJECTLEVEL4",url);
            Log.d("RECEIVEJSONOBJECTLEVEL4",description);
            Log.d("RECEIVEJSONOBJECTLEVEL4",publishedDate);
            NewsObj aObj = new NewsObj(title,url,publishedDate);
            newsList.add(aObj);

我似乎无法从mediaGroup中找到任何价值。请帮帮忙

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-28 12:56:52

若要访问内容数组,请执行以下操作:

代码语言:javascript
复制
JSONArray contents = jsonArray1.getJSONObject(i).getJSONArray("mediaGroups").getJSONObject(0).getJSONArray("contents");

然后:

代码语言:javascript
复制
String url = contents.getJSONObject(i).getString("url");

若要访问缩略图数组,请执行以下操作:

代码语言:javascript
复制
JSONArray thumbnails = contents.getJSONObject(i).getJSONArray("thumbnails");
String thumbNailURL = thumbnails.getJSONObject(0).getString("url");
票数 0
EN

Stack Overflow用户

发布于 2016-05-28 12:35:55

mediaGroup是JsonArray而不是JsonObject。要访问它,请执行如下操作

代码语言:javascript
复制
JSONObject jsonObj= jsonArray1.getJSONObject(i);
JSONArray media=jsonObj.getJSONArray("mediaGroups");

你的下一个问题是

代码语言:javascript
复制
String entries = jsonObject3.getString("entries");

条目也是一个json,所以您也必须得到它

代码语言:javascript
复制
JSONArray entries=jsonObject3.getJSONArray("entries");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37498850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档