如何使用json解析获得价值。

时间:2011-12-30 12:09:39

标签: android json

我在android工作。我想显示一个用户签到列表。

我以json的形式得到我的结果。现在我想解析这个。

以下是我的输出。: -

{
   "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

提前谢谢。

1 个答案:

答案 0 :(得分: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();
}