简体   繁体   English

JAVA中JsonObjects的JsonObject

[英]JsonObject of JsonObjects in JAVA

I have a JSONObject full of JSONobject s, and I need to extract each of those into a new JSONObject so I can manipulate each of them individually, but I don't really know how to do it.我有一个充满JSONobjectJSONObject ,我需要将它们中的每一个提取到一个新的JSONObject以便我可以单独操作它们中的每一个,但我真的不知道该怎么做。 My code is like this:我的代码是这样的:

public void loadBodies(InputStream in) {

JSONObject jsonInput= new JSONObject(new JSONTokener(in));

JSONObject jo2 = jsonInput.getJSONObject("bodies"); //probably incorrect
for(JSonObject j: jo2) b.create(j); //i need to apply the create method to all the JSONObjects

imagine the JSON like this想象这样的 JSON

{'bodies': [
        {
            'total': 142250.0, 
            '_id': 'BC'
        }, 
        {
            'total': 210.88999999999996,
             '_id': 'USD'
        }, 

        {
            'total': 1065600.0, 
            '_id': 'TK'
        }
        ]
}

I need to extract all the JSONObject s under the key bodies into a new set of JSONObjects , so I can operate with them.我需要将 key bodies下的所有JSONObject提取到一组新的JSONObjects ,以便我可以对它们进行操作。 So basically, extract them in a loop, but I don't know how to.所以基本上,在循环中提取它们,但我不知道如何。

According to your example, bodies is a JSON array.根据您的示例, bodies是一个 JSON 数组。 So use JSONArray of the org.json:json library in order to iterate over the array's content:所以使用org.json:json库的JSONArray来迭代数组的内容:

String     json      = "{\"bodies\": [{\"total\": 142250.0, \"_id\": \"BC\"}]}";

JSONObject jsonInput = new JSONObject(new JSONTokener(new StringReader(json)));
JSONArray  array     = jsonInput.getJSONArray("bodies");
for (int i = 0; i < array.length(); i++) {
    JSONObject obj = array.getJSONObject(i);
     // implement here your logic on obj
}

You can do like this and manipulate each value according to your requirement .您可以这样做并根据您的要求操作每个值。 This method calls recursively every time when it finds any JSONObject or JSONArray inside object.每次在对象中找到任何 JSONObject 或 JSONArray 时,此方法都会递归调用。 handleValue() method for manipulating value for specific key. handleValue() 方法用于操作特定键的值。

public void handleJSON(Object input, Map<String,Object> result ){
if (input instanceof JSONObject) {
  List < String > keys = new ArrayList < > (((JSONObject) input).keySet());
  for (String key: keys) {
    if (!(((JSONObject) input).get(key) instanceof JSONArray))
      if (((JSONObject) input).get(key) instanceof JSONObject) {
        handleJSONObject(((JSONObject) input).get(key), map);
      } else {
        Object value = ((JSONObject) input).get(key);
        ((JSONObject) input).put(key, handleValue(value, map));
      }
    else
      handleJSONObject(new JSONArray(((JSONObject) input).get(key).toString()), map);
  }

}
if (input instanceof JSONArray) {
  for (int i = 0; i < ((JSONArray) input).length(); i++) {
    JSONObject jsonObject = ((JSONArray) input).getJSONObject(i);
    handleJSONObject(jsonObject, map);
  }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM