我使用一个名为JSONObject的JSON库(如果需要,我不介意切换)。
我知道如何遍历JSONArrays,但当我从Facebook解析JSON数据时,我没有得到一个数组,只有一个JSONObject,但我需要能够通过它的索引访问一个项目,如JSONObject[0]来获得第一个,我不知道如何做到这一点。
{
"http://http://url.com/": {
"id": "http://http://url.com//"
},
"http://url2.co/": {
"id": "http://url2.com//",
"shares": 16
}
,
"http://url3.com/": {
"id": "http://url3.com//",
"shares": 16
}
}
这里的大多数答案都是扁平的JSON结构,如果你有一个JSON,可能有嵌套的JSONArrays或嵌套的JSONObjects,真正的复杂性就出现了。下面的代码片段处理这样的业务需求。它接受一个哈希映射和带有嵌套JSONArrays和JSONObjects的分层JSON,并使用哈希映射中的数据更新JSON
public void updateData(JSONObject fullResponse, HashMap<String, String> mapToUpdate) {
fullResponse.keySet().forEach(keyStr -> {
Object keyvalue = fullResponse.get(keyStr);
if (keyvalue instanceof JSONArray) {
updateData(((JSONArray) keyvalue).getJSONObject(0), mapToUpdate);
} else if (keyvalue instanceof JSONObject) {
updateData((JSONObject) keyvalue, mapToUpdate);
} else {
// System.out.println("key: " + keyStr + " value: " + keyvalue);
if (mapToUpdate.containsKey(keyStr)) {
fullResponse.put(keyStr, mapToUpdate.get(keyStr));
}
}
});
}
您必须注意到这里this的返回类型是void,但是sice对象作为引用传递,此更改反映给调用者。
我将避免迭代器,因为它们可以在迭代过程中添加/删除对象,也可以为循环使用干净的代码。它将是简单的干净和更少的行。
使用Java 8和Lamda[更新4/2/2019]
import org.json.JSONObject;
public static void printJsonObject(JSONObject jsonObj) {
jsonObj.keySet().forEach(keyStr ->
{
Object keyvalue = jsonObj.get(keyStr);
System.out.println("key: "+ keyStr + " value: " + keyvalue);
//for nested objects iteration if required
//if (keyvalue instanceof JSONObject)
// printJsonObject((JSONObject)keyvalue);
});
}
使用旧方式[更新4/2/2019]
import org.json.JSONObject;
public static void printJsonObject(JSONObject jsonObj) {
for (String keyStr : jsonObj.keySet()) {
Object keyvalue = jsonObj.get(keyStr);
//Print key and value
System.out.println("key: "+ keyStr + " value: " + keyvalue);
//for nested objects iteration if required
//if (keyvalue instanceof JSONObject)
// printJsonObject((JSONObject)keyvalue);
}
}
原来的答案
import org.json.simple.JSONObject;
public static void printJsonObject(JSONObject jsonObj) {
for (Object key : jsonObj.keySet()) {
//based on you key types
String keyStr = (String)key;
Object keyvalue = jsonObj.get(keyStr);
//Print key and value
System.out.println("key: "+ keyStr + " value: " + keyvalue);
//for nested objects iteration if required
if (keyvalue instanceof JSONObject)
printJsonObject((JSONObject)keyvalue);
}
}
我做了一个小的递归函数,遍历整个json对象并保存键路径及其值。
// My stored keys and values from the json object
HashMap<String,String> myKeyValues = new HashMap<String,String>();
// Used for constructing the path to the key in the json object
Stack<String> key_path = new Stack<String>();
// Recursive function that goes through a json object and stores
// its key and values in the hashmap
private void loadJson(JSONObject json){
Iterator<?> json_keys = json.keys();
while( json_keys.hasNext() ){
String json_key = (String)json_keys.next();
try{
key_path.push(json_key);
loadJson(json.getJSONObject(json_key));
}catch (JSONException e){
// Build the path to the key
String key = "";
for(String sub_key: key_path){
key += sub_key+".";
}
key = key.substring(0,key.length()-1);
System.out.println(key+": "+json.getString(json_key));
key_path.pop();
myKeyValues.put(key, json.getString(json_key));
}
}
if(key_path.size() > 0){
key_path.pop();
}
}