无法迭代JSONObject

时间:2017-01-01 18:48:15

标签: java json

[已解决]

private  void jsonObjectToMap(JSONObject jsonObject) throws JSONException {

        Map<String,String> map = new HashMap<String,String>();
        Iterator iter = jsonObject.keys();
        while(iter.hasNext()) {
            String key = (String) iter.next();
            JSONObject value = jsonObject.getJSONObject(key);
            Iterator iter2 = value.keys();
            while (iter2.hasNext()) {
                String key2 = (String) iter2.next();
                Object value2 = value.get(key2);
                map.put(key2,value.toString());
                System.out.println(key2 + " : " + value2);
            }
        }

我有以下JSON(请参阅下面的“json”),使用these methods,我总是"1"key"{"this": 1, "is": 1,"an": 1, "example": 1,"text": 1}value。我希望括号中的每个单词都以keys分隔,相关的数字为value

这是我试过的最后一个代码:

for(int i = 0; i < jsonObject.names().length(); i++){
    System.out.println("key = " + jsonObject.names().getString(i) + 
            " value = " + jsonObject.get(jsonObject.names().getString(i)));
}

JSON:

"1": {
      "this": 1,
      "is": 1,
      "an": 1,
      "example": 1,
      "text": 1
     }

整个JSON,但我隔离了json-object "1"

"output": {
           "status": 1,
           "result": {
                      "1": {
                             "this": 1,
                             "is": 1,
                             "an": 1,
                             "example": 1,
                             "text": 1
                           }
                      } 
           }

0 个答案:

没有答案