使用java获取arraylist文件json

cl25kdpy  于 2021-07-09  发布在  Java
关注(0)|答案(4)|浏览(342)

我的代码有问题,我需要在arraylist中提取“materialas”标记。示例数据:

[{
    "name": "A114",
    "grupo": "DAW2",
    "tutor": 15,
    "materias": ["DWES", "DWEC", "IW", "DAW", "IE"]
}]

我试过了,但我需要数组列表的内容:

try { // this read the JSON file
    line = new String(Files.readAllBytes(Paths.get("fileAulas")));
} catch (Exception ex) {
    ex.printStackTrace();
}
JSONArray recs = new JSONArray(line);
for (Object rec : recs) {

    Aula datos = new Aula();

    JSONObject obj = ((JSONObject) rec);
    String name = obj.getString("name");    
    //datos.setNombre(name);

    String grupo = obj.getString("grupo");
    //datos.setGrupo(grupo);

    int tutor = obj.getInt("tutor");
    //datos.setTutor(tutor);
}

我的目标是阅读数组列表,然后 datos.setArraylist 去我的另一个班。除了读数组列表,其他都可以
pd:我使用从maven下载的java,执行时使用“javac-cp./*:program.java”

dnph8jn4

dnph8jn41#

materias 也是一个数组,因此需要如下内容:

JSONArray materias = obj.getJsonArray("materias");
List<String> materiasList = materias.getValuesAs(String.class);

这不是类型安全的,因此需要确保数组只包含字符串值。
另外,如果您使用的是Java8,那么可以迭代这些值并手动存储它们( JsonArray 也是一个 Collection<JsonValue> )

j2cgzkjk

j2cgzkjk2#

首先要让你的json有效。

[{
        "name": "A114",
        "grupo": "DAW2",
        "tutor": 15,
        "materias": ["DWES", "DWEC", "IW", "DAW", "IE"]
    }]

其次,您可以尝试以下方法:

for (JsonElement rec : recs) {

    Aula datos = new Aula();

    JSONObject obj = rec.getAsJsonObject();
    String name = obj.get("name").getAsString();    
    //datos.setNombre(name);

    String grupo = obj.get("grupo").getAsString();
    //datos.setGrupo(grupo);

    int tutor = obj.get("tutor").getAsInt();

     JsonObject obj = rec.getAsJsonObject();
      JsonArray materias = obj.getAsJsonArray("materias");

          List<String> materiasList = new ArrayList<>();

for (JsonElement item:
                 materias) {
                materiasList.add(item.getAsString());
            }

}
oiopk7p5

oiopk7p53#

快速创建满足您需求的短期测试实现:

String json = "[{ \"name\":\"A114\",\"grupo\": \"DAW2\",\"tutor\":15,\"materias\": [\"DWES\",\"DWEC\",\"IW\",\"DAW\",\"IE\"]}]";

    JSONArray objectArray = new JSONArray(json);

    for (int x = 0; x < objectArray.length(); x++) {
        JSONObject obj = objectArray.getJSONObject(x);

        System.out.println("Name: " + obj.get("name"));
        System.out.println("Grupo: " + obj.get("grupo"));
        System.out.println("Tutor: " + obj.get("tutor"));

        JSONArray materias = obj.getJSONArray("materias");
        for (int y = 0; y < materias.length(); y++) {
            System.out.println("Materia " + y + ": " + materias.get(y));
        }
    }

输出
姓名:a114 grupo:daw2导师:15材料0:dwes材料1:dwec材料2:iw材料3:daw材料4:ie

omvjsjqw

omvjsjqw4#

我相信json中数组的语法与文件中的不同。例如-包含简单元素数组的简单完整json将是:

{"contacts":[
      { "firstName":"John", "lastName":"Doe" },
      { "firstName":"Jen", "lastName":"Smith" },
      { "firstName":"David", "lastName":"Jones" }
    ]}

在文本中,可以指定一个数组,而不需要打开和关闭json“{”和“}”。此外,您还需要命名数组元素(上面示例中的“contacts”)。我认为您想要的是(我使用“arrayname”作为数组元素名):

{"arrayName":[{ "name":"A114","grupo": "DAW2","tutor":15,"materias":["DWES","DWEC","IW","DAW","IE"] }, 
      { "name":"121","grupo": "DAW1","tutor":8,"materias":["PROGRA","BD","SIS","LM","FOL"] }, 
      { "name":"A112","grupo": "AUTO1","tutor":5 }, 
      { "name":"A127","grupo": "ASIR1","tutor":2 }, 
      { "name":"A114","grupo": "SMR2","tutor":11 }, 
      { "name":"A128","grupo": "ASIR2","tutor":9,"materias":["IAW","ABD","ASIS","SI","IE"] }, 
      { "name":"A125","grupo": "ADM2","tutor":12 } ] }

相关问题