动态读取具有不同名称的多个 JSON 数组。

huangapple go评论69阅读模式
英文:

Read multiple json array with different names dynamically

问题

package com.abc;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class JsonRead {

    private static final String filePath = "jsonTestFile.json";

    public static void main(String[] args) {

        try (FileReader reader = new FileReader(ClassLoader.getSystemResource(filePath).getFile())) {

            JSONParser jsonParser = new JSONParser();
            JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);

            Iterator<?> iterator = jsonObject.keySet().iterator();  // Changed variable name

            while (iterator.hasNext()) {
                String key = (String) iterator.next();
                JSONArray jsonArray = (JSONArray) jsonObject.get(key);

                Iterator<?> arrayIterator = jsonArray.iterator();
                while (arrayIterator.hasNext()) {
                    JSONObject innerObj = (JSONObject) arrayIterator.next();
                    System.out.println("high " + innerObj.get("high") + " low " + innerObj.get("low") + " key " + innerObj.get("key"));
                }
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}
英文:

I have a JSON which has many arrays with a different name, just like below JSON.

{
&quot;CA&quot;: [
{
&quot;high&quot;: 5,
&quot;low&quot;: 3,
&quot;key&quot;: &quot;ABPS&quot;
},
{
&quot;high&quot;: 6,
&quot;low&quot;: 2,
&quot;key&quot;: &quot;ABPO&quot;
}
],
&quot;EE&quot;: [
{
&quot;high&quot;: 8,
&quot;low&quot;: 4,
&quot;key&quot;: &quot;ABPS&quot;
},
{
&quot;high&quot;: 7,
&quot;low&quot;: 2,
&quot;key&quot;: &quot;ABPO&quot;
}
]
}

I am trying to iterate JSON array values dynamically without specifying the name of the array.

I am able to read array with specifying the name of the array with below code but how to read array values dynamically without specifying the name of every array because the JSON file I have it has thousands of array.

package com.abc;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class JsonRead {
private static final String filePath = &quot;jsonTestFile.json&quot;;
public static void main(String[] args) {
try (FileReader reader = new FileReader(ClassLoader.getSystemResource(filePath).getFile())) {
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
// get an array from the JSON object
JSONArray lang = (JSONArray) jsonObject.get(&quot;CA&quot;);
Iterator i = lang.iterator();
// take each value from the json array separately
while (i.hasNext()) {
JSONObject innerObj = (JSONObject) i.next();
System.out.println(&quot;high &quot; + innerObj.get(&quot;high&quot;) + &quot; low &quot; + innerObj.get(&quot;low&quot;)+ &quot; key &quot; + innerObj.get(&quot;key&quot;));
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

答案1

得分: 0

这可能会有所帮助

for (Iterator iterator = jsonObject.keySet().iterator(); iterator.hasNext();) {
String key = (String) iterator.next();
JSONArray jArray = (JSONArray) jsonObject.get(key);
}
英文:

This might help

for (Iterator iterator = jsonObject.keySet().iterator(); iterator.hasNext();) {
String key = (String) iterator.next();
JSONArray jArray = (JSONArray) jsonObject.get(key);
}

huangapple
  • 本文由 发表于 2020年5月5日 18:00:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/61610479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定