使用gson在java中循环json对象

使用gson在java中循环json对象,java,json,gson,Java,Json,Gson,下面是以json格式存储在json文件中的设备详细信息列表 {"Device_details":[ {"DUT1": {"Interface":"eth1", "IP_address":"40.0.0.1/24"}, "DUT3": {"Interface":"eth3", "IP_address":"40.0.0.3/24"}, "DUT2": {"Interface":"eth2",

下面是以json格式存储在json文件中的设备详细信息列表

{"Device_details":[
    {"DUT1":
        {"Interface":"eth1",
        "IP_address":"40.0.0.1/24"},
     "DUT3":
        {"Interface":"eth3",
        "IP_address":"40.0.0.3/24"},
     "DUT2":
        {"Interface":"eth2",
        "IP_address":"40.0.0.2/24"
        }
    }
             ]}
我想循环遍历每个设备,并在对应于单个DUT的列表中打印出每个设备的接口和ip地址

样本输出:

Device : DUT1
Interface : eth1
IP_address : 40.0.0.1/24

Device : DUT2
Interface : eth2
IP_address : 40.0.0.2/24
等等。。。。 显示设备的顺序无关紧要,我想使用GSON来实现这一点。 到目前为止,我的代码是:

JsonParser parser = new JsonParser();
JsonElement jsonElement = parser.parse(new FileReader(fileName.json));
jsonObject = jsonElement.getAsJsonObject();
JsonArray devices = jsonObject.getAsJsonArray("Device_details");
JsonElement device_list = devices.get(0);
JsonObject devices_object= device_list.getAsJsonObject();
System.out.println(devices_object.size());
for (int i=0; i < devices_object.size(); i++){
//How do I access the devices and it's details
        };
JsonParser=newjsonparser();
JsonElement=parser.parse(新文件阅读器(fileName.json));
jsonObject=jsonElement.getAsJsonObject();
JsonArray devices=jsonObject.getAsJsonArray(“设备详细信息”);
JsonElement device_list=devices.get(0);
JsonObject devices_object=device_list.getAsJsonObject();
System.out.println(devices_object.size());
对于(int i=0;i
公共静态void main(字符串[]args)引发FileNotFoundException{
Gson Gson=新的Gson();
JsonReader=newJSONReader(newFileReader(“pathToJson.json”);
Response res=gson.fromJson(reader,Response.class);
用于(地图:res.Device\u详细信息){
对于(Map.Entry e:Map.entrySet()){
System.out.println(“设备:+e.getKey());
设备设备=e.getValue();
System.out.println(“接口:“+设备接口”);
System.out.println(“IP地址:+设备IP地址”);
}
}
}
类设备{
字符串接口;
字符串IP_地址;
}
班级反应{
List Device_details=new ArrayList();
}
JsonReader
gson.fromJson
允许您将json映射到java对象中


这里,
Response
是“根元素”。其属性
Device\u details
必须与json中的名称相同。

您的json是否始终具有相同的结构?您是否检查了如何使用GSON访问数据?教程是一个良好的开端;)@是的。JSON将始终具有相同的结构。只是设备一直在添加。@AxelH我确实检查了,但没有找到循环thorugh JSON对象的方法,就像我的例子一样,您可能会发现这个示例很有用。请看“嵌套JSON对象”部分。如果结构始终相同,则可以创建与JSON匹配的类。这将使您的代码更简单,因为您在解析一个普通的java对象之后正在使用它。这段代码对我来说很好,而且非常新,但是输出是完全不同的。Key:DUT3接口:eth3 IP_地址:40.0.0.3/24接口:eth1 IP_地址:40.0.0.1/24接口:eth2 IP_地址:40.0.0.2/24 Key:DUT1接口:eth3 IP_地址:40.0.0.3/24接口:eth1 IP_地址:40.0.0.1/24接口:eth2 IP_地址:40.0.0.2/24打印出给定密钥的所有设备的接口详细信息。但是,它应该只打印相应设备的详细信息。在您的问题中:
我想循环每个设备,并打印出接口和ip地址。
。所以,我不明白你想要什么。请更新您的问题以添加预期输出,好吗?你所说的相应设备是什么意思?记住,这不一定是准确的答案。想法就在那里,OP可以从中学习,以适应他的需要。PS:如果可以的话,至少应该对定义的类添加一些解释。
public static void main(String[] args) throws FileNotFoundException {
    Gson gson = new Gson();
    JsonReader reader = new JsonReader(new FileReader("pathToJson.json"));
    Response res = gson.fromJson(reader, Response.class);
    for (Map<String, Device> map : res.Device_details) {
        for (Map.Entry<String,Device> e : map.entrySet()){
            System.out.println("Device : " + e.getKey());
            Device device = e.getValue();
            System.out.println("Interface : " + device.Interface);
            System.out.println("IP_address : " + device.IP_address);
        }
    }
}

class Device {
    String Interface;
    String IP_address;
}


class Response {
    List<HashMap<String, Device>> Device_details = new ArrayList();
}