java JsonObject遍历

2022-03-21 11:59:54 浏览数 (1)

代码语言:javascript复制
       for(String key:jsonObject.keySet()){
                uriBuilder.setParameter(key, jsonObject.get(key).toString());
            }
代码语言:javascript复制
for (Map.Entry entry : jsobject.entrySet()) {
System.out.println(“key值=” entry.getKey());
System.out.println(“value=” entry.getValue());
}
代码语言:javascript复制
private static String getDesc(String jsonStr, String key) {
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        for (Map.Entry entry : jsonObject.entrySet()) {
            if (key.equals(entry.getKey())) {
                return entry.getValue().toString();
            }
        }
        return null;
    }
代码语言:javascript复制
JSONObject jsonObject = new JSONObject(contents.trim());
Iterator<String> keys = jsonObject.keys();

while(keys.hasNext()) {
    String key = keys.next();
    if (jsonObject.get(key) instanceof JSONObject) {
          // do something with jsonObject here      
    }
}

方法: Iterator iter = jsonInfo.entrySet().iterator();

代码示例:

代码语言:javascript复制
public class Test {
    public static void main(String[] args) {

        JSONObject jsonInfo = new JSONObject();

        String key1 = "a";
        jsonInfo.put(key1, "aa");
        String key2 = "b";
        jsonInfo.put(key2, "bb");

        Iterator iter = jsonInfo.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            System.out.println(entry.getKey().toString());
            System.out.println(entry.getValue().toString());
        }
    }
}

0 人点赞