0

如何解析这段 JSON 代码?

{
  "direction": "ltr",
  "id": "feed/http => //www.theverge.com/rss/full.xml",
  "title": "The Verge -  All Posts",
  "continuation": "CLKM0OyU0rYC",
  "self": [
    {
      " href": "https => //cloud.feedly.com/reader/3/stream/contents/feed%2Fhttp%3A%2F%2Fwww.theverge.com%2Frss%2Ffull.xml?n=20&unreadOnly=true"
    }
  ],
  "alternate": [
    {
      "href": "http://www.theverge.com/",
      "type": "text/html"
    }
  ],
  "updated": 1367539068016,
  "items": [
    {
      "id": "entryId",
      "unread": true,
      "categories": [
        {
          "id": "user/c805fcbf-3acf-4302-a97e-d82f9d7c897f/category/tech",
          "label": "tech"
        }
      ],
      "tags": [
        {
          "id": "user/c805fcbf-3acf-4302-a97e-d82f9d7c897f/tag/inspiration",
          "label": "inspiration"
        }
      ],
      "title": "NBC's reviled sci-fi drama 'Heroes' may get a second lease on life as Xbox Live exclusive",
      "published": 1367539068016,
      "updated": 1367539068016,
      "crawled": 1367539068016,
      "alternate": [
        {
          "href": "http://www.theverge.com/2013/4/17/4236096/nbc-heroes-may-get-a-second-lease-on-life-on-xbox-live",
          "type": "text/html"
        }
      ],
      "content": {
        "direction": "ltr",
        "content": "..."
      },
      "author": "Nathan Ingraham",
      "origin": {
        "streamId": "feed/http://www.theverge.com/rss/full.xml",
        "title": "The Verge -  All Posts",
        "htmlUrl": "http://www.theverge.com/"
      },
      "engagement": 15
    },
    {
      "id": "entryId2",
      "unread": true,
      "categories": [
        {
          "id": "user/c805fcbf-3acf-4302-a97e-d82f9d7c897f/category/tech",
          "label": "tech"
        }
      ],
      "tags": [
        {
          "id": "user/c805fcbf-3acf-4302-a97e-d82f9d7c897f/tag/inspiration",
          "label": "inspiration"
        }
      ],
      "title": "Senate rejects bipartisan gun control measure for background checks despite broad public support",
      "published": 1367539068016,
      "updated": 1367539068016,
      "crawled": 1367539068016,
      "alternate": [
        {
          "href": "http://www.theverge.com/2013/4/17/4236136/senate-rejects-gun-control-amendment",
          "type": "text/html"
        }
      ],
      "content": {
        "direction": "ltr",
        "content": "...html content..."
      },
      "author": "T.C. Sottek",
      "origin": {
        "streamId": "feed/http://www.theverge.com/rss/full.xml",
        "title": "The Verge -  All Posts",
        "htmlUrl": "http://www.theverge.com/"
      },
      "engagement": 39
    }
  ]
}

那是我的解决方案,但它不起作用......我的错误是什么?谢谢

    try{
        //JSONArray elements = new JSONArray (response);
        JSONObject json=new JSONObject(response);
        JSONArray elements = json.getJSONArray("items");

        Log.d(TAG, "Elemenenti numero" +elements.length());

        // Getting Array of Contacts


        // looping through All Contacts
        for(int i = 0; i < elements.length(); i++){
            JSONObject c = elements.getJSONObject(i);



            // Storing each json item in variable

            String identifier = c.getString("id");
            String title = c.getString("title");
            String link = c.getString("originId");
            String data = c.getString("published");
            SimpleDateFormat  format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");  
            Date date=new Date();

            try {  
                date = format.parse(data);  
                System.out.println(date);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }


            JSONObject summaryObj= c.getJSONObject("summary");
            String summary = summaryObj.getString("content");

            JSONObject contentObj= c.getJSONObject("content");
            String content = contentObj.getString("content");

            JSONObject sourceObj= c.getJSONObject("origin");
            String source = contentObj.getString("title");


            if (summary.length()==0 && content.length()!=0) summary=content;
            if (content.length()==0 && summary.length()!=0) content=summary;

            String image=this.getFirstImage(content);

            FeedItem toAdd=new FeedItem(identifier, title, link, date, null, summary, content, image, source);

            toAdd.toString();



        }


    }catch (JSONException e) {
        e.printStackTrace();
    }
4

2 回答 2

0

您没有任何名为“originID”的标签。但是您正试图从中获取 String 。

同样,您也没有标签“summary”,但您正试图从中获取 JSONObject。

于 2013-08-23T11:46:10.093 回答
0
JSONObject summaryObj= c.getJSONObject("summary");

没有叫summary的元素,你可以试试

if(c.has("summary")) {
    JSONObject summaryObj= c.getJSONObject("summary");
}

如果这不起作用,请发布您的堆栈跟踪,(logcat)

于 2013-08-23T11:42:26.703 回答