在不知道其名称Java的情况下获取JsonObject

时间:2015-08-28 17:31:41

标签: java json for-loop

我需要解析这个JSON。它的格式看起来像这样:

{
   "47M": [
           {
              "lat": 39.95507, 
              "lng": -75.152122, 
              "label": 8011, 
              "VehicleID": 8011, 
              "BlockID": 7995, 
              "Direction": "NorthBound", 
              "destination": "Spring Garden via 9th St.", 
              "Offset": 1, 
              "Offset_sec": 29
          }, 
          {
              "lat": 39.913765, 
              "lng": -75.155464, 
              "label": 8038, 
              "VehicleID": 8038, 
              "BlockID": 7993, 
              "Direction": "NorthBound", 
              "destination": "Spring Garden via 9th St.", 
              "Offset": 3, 
              "Offset_sec": 158
          }
     ]
} 

然而,那" 47M"可以是" 5"," H"," 101"等等。我需要得到那些" 5"," H"等 我相信我应该使用循环for (obj : root),但我不知道obj

的类型是什么

1 个答案:

答案 0 :(得分:0)

您可以使用简单的json(https://code.google.com/p/json-simple/)库和这样的代码来迭代键。

JSONParser parser = new JSONParser();

jObject = parser.parse(jsonString);
JSONObject jsonObject = (JSONObject) jObject;
for(Iterator iterator = jsonObject.keySet().iterator(); iterator.hasNext();) {
    String key = (String) iterator.next();
    System.out.println(jsonObject.get(key));
}