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

要約という要素はありません。試してみてください

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

それでもうまくいかない場合は、スタックトレースを投稿してください (logcat)

于 2013-08-23T11:42:26.703 に答える