简体   繁体   English

在Android中解析Json String(Feed RSS文件)

[英]Parse Json String (Feed RSS file) in Android

How can I parse this piece of JSON code? 如何解析这段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
    }
  ]
}

That is my solution but it doesn't work... what is my error? 那是我的解决方案,但不起作用...我的错误是什么? thanks 谢谢

    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();
    }
JSONObject summaryObj= c.getJSONObject("summary");

There is no element called summary, you may try 没有称为摘要的元素,您可以尝试

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

if that doesn't work, please post your stacktrace, (logcat) 如果那不起作用,请发布您的堆栈跟踪,(logcat)

You don't have any tag named "originID". 您没有任何名为“ originID”的标签。 but you are trying to get String from it. 但是您正在尝试从中获取String。

Similarly,you don't have tag "summary" also but you are trying to get JSONObject from it. 同样,您也没有标签“ summary”,但是您尝试从中获取JSONObject。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM