Java 处理Json

Published: 04 Dec 2018 Category: java

1. 解析 json 遍历未知key

解析未知key的json串,例如:

......
"extension": {
    "pit_25": "0",
    "pit_26": "1",
    "pit_27": "0",  
    "pit_41": "0"
}

1.1 代码

...
JSONObject maleArray = maleObject.getJSONObject("extension");

Iterator<String> male_Iterator = maleArray.keys();

while(male_Iterator.hasNext()){
    // 获得key 
    String key = male_Iterator.next(); 
    //根据key获得value, value也可以是JSONObject,JSONArray,使用对应的参数接收即可 
    String value = maleArray.getString(key); 
    ...
}
...