2017-04-11 63 views
-2

我有这样的JSON:如何检查JSON是否嵌套在Java中?

{ 
    "id": 2643743, 
    "dt": 1485789600, 
    "clouds": { 
     "all": 90 
    } 
} 

在这个JSON,我怎么可以检查,有一个嵌套的JSON的关键?例如,我如何检查云是否嵌套了JSON?

JSONObject json = readJsonFromUrl("http://samples.openweathermap.org/data/2.5/weather?q=London,uk&APPID=916c290adb437e1cc53eab01798225ed"); 
Iterator keys = json.keys(); 
Iterator inskeys = json.keys(); 
String keyStr = null; 
while(keys.hasNext()) { 
    keyStr = (String) keys.next(); 
    Object keyvalue = json.get(keyStr); 
    Object keyType = keyvalue.getClass().getSimpleName(); 
    ... 
+2

帖子你尝试过什么。至少说出你正在使用的几十个JSON解析API中的哪一个。 –

+0

那么,为什么不检查价值的类型? – Henry

回答

0

下面是简单的例子:

String JSON = "{\"id\":2643743,\"dt\":1485789600,\"clouds\":{\"all\":90}}";  
    JSONObject jsonObject = new JSONObject(JSON); 
    JSONObject getSth = jsonObject.getJSONObject("clouds"); 
+0

你忘了逃避双引号。 –

+0

根据他用于JSON解析的库,这很可能是不正确的。对于JEE,它应该可以工作。 –