gson 获取嵌套json对象的键值

kmpatx3s  于 2022-11-06  发布在  其他
关注(0)|答案(3)|浏览(201)

我想使用org.json库获取下一个json文件的键值:

{
    "1": {
        "subject1": "MIS",
        "subject2": "DBMS",
        "subject3": "UML"
    },
    "2": {
        "subject1": "ART",
        "subject2": "MATH",
        "subject3": "MUSIC"
    },
    "3": {
        "subject1": "HISTORY",
        "subject2": "CHEMISTY",
        "subject3": "BIOLOGY"
    }
}

这就是我所尝试的:

package com.company;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.FileReader;

public class Main {
    public static void main(String[] args) {
        String json = "{\"1\": {\"subject1\": \"MIS\",\"subject2\": \"DBMS\",\"subject3\": \"UML\"},\"2\": {\"subject1\": \"ART\",\"subject2\": \"MATH\",\"subject3\": \"MUSIC\"},\"3\": {\"subject1\": \"HISTORY\",\"subject2\": \"CHEMISTY\",\"subject3\": \"BIOLOGY\"}}";

        try{
            JSONObject root = new JSONObject(json);
            for (int i = 0; i < root.length(); i++) {
                JSONArray subjects = root.getJSONArray(String.valueOf(i+1));

                for (int j = 0; j < subjects.length(); j++) {
                    JSONObject number = subjects.getJSONObject(j);
                    String s1 = number.getString("subject1");
                    String s2 = number.getString("subject2");
                    String s3 = number.getString("subject3");
                    System.out.println(s1+", "+ s2+", "+s3);
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

使用JSONArray subjects = root.getJSONArray(String.valueOf(i+1));时,我认为我得到了:

JSONArray subjects = root.getJSONArray("1");
JSONArray subjects = root.getJSONArray("2");
...

但这对我不起作用。我只是在控制台下:

org.json.JSONException: JSONObject["1"] is not a JSONArray.
    at org.json.JSONObject.wrongValueFormatException(JSONObject.java:2694)
    at org.json.JSONObject.getJSONArray(JSONObject.java:777)
    at com.company.Main.main(Main.java:18)

我想得到:

MIS, DBMS, UML
ART, MATH, MUSIC
HISTORI, CHEMISTRY, BIOLOGY

你能告诉我我做错了什么吗?我应该使用其他库吗?谢谢你的帮助。

kyks70gy

kyks70gy1#

到现在为止你都说对了

JSONArray subjects = root.getJSONArray(String.valueOf(i+1));

你必须得到JSONObject而不是JSONArray,因为你没有数组。

for (int j = 0; j < subjects.length(); j++) {}

因为您已经手动获取了对象,所以不必循环。
最终代码

String json = "{\"1\": {\"subject1\": \"MIS\",\"subject2\": \"DBMS\",\"subject3\": \"UML\"},\"2\": {\"subject1\": \"ART\",\"subject2\": \"MATH\",\"subject3\": \"MUSIC\"},\"3\": {\"subject1\": \"HISTORY\",\"subject2\": \"CHEMISTY\",\"subject3\": \"BIOLOGY\"}}";

    try {
        JSONObject root = new JSONObject(json);
        for (int i = 0; i < root.length(); i++) {
            JSONObject number = root.getJSONObject(String.valueOf(i + 1));
            String s1 = number.getString("subject1");
            String s2 = number.getString("subject2");
            String s3 = number.getString("subject3");
            System.out.println(s1 + ", " + s2 + ", " + s3);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

输出

MIS, DBMS, UML
ART, MATH, MUSIC
HISTORY, CHEMISTY, BIOLOGY
wz1wpwve

wz1wpwve2#

Subjects不是数组。JSON中的数组将包含在[]中。要获取subjects,您需要遵循以下方法。

try{
     JSONObject root = new JSONObject(json);
     for (int i = 0; i < 3; i++) {
        JSONObject Obj = root.getJSONObject(String.valueOf(i+1));
        String s1 = Obj.getString("subject1");
        String s2 = Obj.getString("subject2");
        String s3 = Obj.getString("subject3");
        System.out.println(s1+", "+ s2+", "+s3);
      }
  }catch(Exception e){
            e.printStackTrace();
  }

上面的JSON在像下面这样修改时,会变成json数组[{"subject1": "MIS","subject2": "DBMS","subject3": "UML"},{"subject1": "ART","subject2": "MATH","subject3": "MUSIC"},{"subject1": "HISTORY","subject2": "CHEMISTY","subject3": "BIOLOGY"}]
然后可以如下所述提取值,

try{
    JSONArray root = new JSONArray(json);

    for (int i = 0; i < root.length(); i++) {
       JSONObject Obj = root.getJSONObject(i);
       String s1 = Obj.getString("subject1");
       String s2 = Obj.getString("subject2");
       String s3 = Obj.getString("subject3");
       System.out.println(s1+", "+ s2+", "+s3);
   }
}catch(Exception e){
  e.printStackTrace();
}
9w11ddsr

9w11ddsr3#

在Java中,从嵌套JSON格式的文件中获取数据的代码会很长。
您可以使用SPL(开放源代码的Java包)来完成此操作。它很简单,只需一行代码即可:
| | A级|
| - -|- -|
| 一个|=json(文件(“org.json”).读取()).数组()|
SPL提供了Java调用的JDBC驱动程序,只需将上面的SPL脚本存储为getjsonarray.splx,在调用存储过程时在Java中调用即可:

…
Class.forName("com.esproc.jdbc.InternalDriver");
con= DriverManager.getConnection("jdbc:esproc:local://");
st = con.prepareCall("call getjsonarray()");
st.execute();
…

相关问题