2011-12-30 46 views
0

我正在android中工作。我想顯示用戶的登記清單。我如何使用json解析來獲得價值?

我得到了我的結果json的形式。現在我想解析這個。

這下面是我output.:-

{ 
    "meta":   
    { 
     "code":200,  
     "errorType":"deprecated",  
     "errorDetail":"" 
    }, 

    "notifications": [ 
     { 
     "type":"notificationTray",  
     "item":{ 
        "unreadCount":0 
     } 
     } 
    ], 
    "response":{ 

     "groups":[  
     { 
      "type":"nearby", 
      "name":"Nearby", 
      "items":[ 
       { 
        "id":"4ed0c8f48231b9ef88fe5f09", 
        "name":"Banayan Tree School", 
        "contact":{ 

        }, 
        "location":{ 
        "lat":26.857954980225713, 
        "lng":75.76602927296061, 
        "distance":510 
        }, 
        "categories":[ 
        { 
         "id":"4bf58dd8d48988d1a8941735", 
         "name":"General College & University", 
         "pluralName":"General Colleges & Universities", 
         "shortName":"Other - Education", 
         "icon":"https:\/\/foursquare.com\/img\/categories\/education\/default.png", 
         "parents":[ 
          "Colleges & Universities" 
         ], 
         "primary":true 
        } 
        ], 
        "verified":false, 
        "stats":{ 
        "checkinsCount":5, 
        "usersCount":4, 
        "tipCount":0 
        }, 
        "hereNow":{ 
        "count":0 
        } 
       }, 
       { 
        "id":"4e75f61fae60c32851596918", 
        "name":"Rajat Path", 
        "contact":{ 

        }, 
        "location":{ 
        "lat":26.866031, 
        "lng":75.759431, 
        "distance":701, 
        "city":"Jaipur", 
        "state":"Rajasthan" 
        }, 
        "categories":[ 
        { 
         "id":"4d4b7105d754a06378d81259", 
         "name":"Shop & Service", 
         "pluralName":"Shops & Services", 
         "shortName":"Shops", 
         "icon":"https:\/\/foursquare.com\/img\/categories\/shops\/default.png", 
         "parents":[ 

         ], 
         "primary":true 
        } 
        ], 
        "verified":false, 
        "stats":{ 
        "checkinsCount":3, 
        "usersCount":3, 
        "tipCount":0 
        }, 
        "hereNow":{ 
        "count":0 
        } 
    } 
      ] 
     } 
     ] 
    } 
} 

請告訴我,我怎樣才能得到checkinscount在我的陣列。

預先感謝您。

+0

哥們,格式化您的文章。沒有人會讀這個。 – Flo 2011-12-30 12:12:21

+1

考慮使用[JSON格式化程序](http://jsonformatter.curiousconcept.com/)。 – Jens 2011-12-30 12:15:18

+0

謝謝Jens ...非常感謝你...謝謝Flo ... – 2011-12-30 12:22:40

回答

1

下面是使用Jackson的一種方式,這是恕我直言最好的JSON庫在那裏:

final ObjectMapper mapper = new ObjectMapper(); 

final JsonNode input = mapper.readTree(...); // fill as appropriate 

final JsonNode items = input.path("response").path("groups").path("items"); 

if (!items.isArray()) 
    // bad input 

int total = 0; 
JsonNode checkinscount; 

// Cycle through array elements -- this works since JsonNode implements Iterable<JsonNode> 
for (final JsonNode element: items) { 
    checkinscount = element.path("stats").path("checkinscount"); 
    if (!checkinscount.isInt()) 
     // bad input 
    total += checkinscount.getIntValue(); 
} 
+0

謝謝fge ... – 2011-12-30 12:56:11