Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果有太多的二级字段,如何解析嵌套的json文件?_Java_Gson - Fatal编程技术网

Java 如果有太多的二级字段,如何解析嵌套的json文件?

Java 如果有太多的二级字段,如何解析嵌套的json文件?,java,gson,Java,Gson,我试图用gson解析一个json文件,我发现了多种解决方案。但我的问题是,我有很多领域?在课堂上把它们都写下来。如何在不创建名称为1-name564的类的情况下获取车内信息+颜色? 下面是一个json示例:'' {"test": {"Name1": {"number":"123", "color":"red", "cars":{"BMW":1, "PORSCHE":2, "MERCEDES":

我试图用gson解析一个json文件,我发现了多种解决方案。但我的问题是,我有很多领域?在课堂上把它们都写下来。如何在不创建名称为1-name564的类的情况下获取车内信息+颜色? 下面是一个json示例:''

{"test":
    {"Name1":
        {"number":"123",
        "color":"red",
        "cars":{"BMW":1,
            "PORSCHE":2,
            "MERCEDES":4,
            "FORD":6}
        },
    .
    .
    .
    .
    .
    .
    "Name564":
        {"number":"143",
        "color":"blue",
        "cars":{"BMW":9,
                "PORSCHE":2,
                "MERCEDES":3,
                "FORD":7}
        }
    }
}

我不确定我是否完全理解了这个问题,但如果我理解了,解决方案可能是创建一个新的Java类,名为say
MyClass
。 MyClass将使用Name1、…、Name564作为实例,使用number、color、cars作为属性,如下所示:

class MyClass {
    private String name;
    private int number; //use int or String if you prefer
    private String color; //you can also use enum instead
    private Map<String, Integer> cars;

    public MyClass(String name) {
        this.name = name;
        cars = new HashMap<>(); // or any other map implementation, depending on the needs
    }

    public void addCar(String model, int numCars) {
        cars.put(model, numCars); // if you always want to replace, add any necessary checks before that
    }

    //or you can use setCars, seeing test.get("cars") as Map
    public void setCars(Map<String,Integer> cars) {
        this.cars = cars;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public void setColor(String color) {
        this.color = color;
    }
}
class-MyClass{
私有字符串名称;
private int number;//如果愿意,请使用int或String
private String color;//也可以改用enum
私家地图车;
公共MyClass(字符串名称){
this.name=名称;
cars=new HashMap();//或任何其他映射实现,具体取决于需要
}
公共void addCar(字符串模型,int numCars){
cars.put(model,numCars);//如果您总是想更换,请在更换之前添加任何必要的检查
}
//或者您可以使用setCars,查看test.get(“cars”)作为地图
公共车辆(地图车){
这个。汽车=汽车;
}
公共无效集合号(整数){
这个数字=数字;
}
公共void setColor(字符串颜色){
这个颜色=颜色;
}
}
然后,在迭代json文档时,可以设置遇到的字段。对于遇到的每个“Name”元素,您可以创建MyClass的新实例:
MyClass currItem=new MyClass(“Name”+counter)
,其中
counter
是从1初始化到“test”元素的子元素大小的计数器

doc.get(“test”).get(“Name”+counter)
将为您提供下一个项目。您可以对相应字段使用
.get(“color”)
.get(“number”)
.get(“cars”)


我希望这有帮助。

您可以使用
Map
进行映射。下面是解析示例的代码:

class JsonRoot {
    Map<String, JsonName> test;
}

class JsonName {
    String number;
    String color;
    Map<String, Integer> cars;
}

...
JsonRoot jsonRoot;
Gson gson = new Gson();
try (BufferedReader reader = Files.newBufferedReader(Paths.get("test.json"))) {
    jsonRoot = gson.fromJson(reader, JsonRoot.class);
}
类JsonRoot{
Map试验;
}
类JsonName{
字符串编号;
字符串颜色;
地图车;
}
...
JsonRoot JsonRoot;
Gson Gson=新的Gson();
try(BufferedReader=Files.newBufferedReader(path.get(“test.json”)){
jsonRoot=gson.fromJson(reader,jsonRoot.class);
}

为什么每个对象都有一个单独的名称?“number”字段不是唯一的吗?我没有创建json文件,也没有创建它的结构。我只是好奇如何解析这样的东西。这将是很难解决的。我也很好奇解决方案会是什么。比我的回答优雅得多:)尝试了你的解决方案,它工作得完美无缺。我甚至没想过地图。非常感谢你