2013-08-23 42 views
0

如何解析這段JSON代碼?在Android中解析Json字符串(Feed RSS文件)

{ 
    "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(); 
    } 

回答

0
JSONObject summaryObj= c.getJSONObject("summary"); 

沒有元素稱爲彙總,您可以嘗試

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

,如果還是不行,請把你的堆棧跟蹤,(logcat的)

0

你不」沒有任何標籤名爲「originID」。但你正試圖從中獲取String。

同樣,你也沒有標籤「摘要」,但你正試圖從中獲取JSONObject。