gpt4 book ai didi

json - 如何在java中解析下面的Json文件,该文件没有 key

转载 作者:行者123 更新时间:2023-12-02 09:29:47 25 4
gpt4 key购买 nike

{
"type": "telemetry",
"timestamp": 1234,
"device": 5678,
"value": {
[
{"battery_level": 87},
{"var2": 2},
{"var3": 3}
]
}
}

我想使用jsonobject在java代码中解析这个json文件和jsonarray在这里,我被困在 value 关键字里面没有数组的键。

最佳答案

您的有效 JSON 是 -

{
"type": "telemetry",
"timestamp": 1234,
"device": 5678,
"value":
[
{"battery_level": 87},
{"var2": 2},
{"var3": 3}
]

}

然后,您可以使用以下 JAVA POJO 对象将 json 解析为 java 对象 -

import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"type",
"timestamp",
"device",
"value"
})
public class Example {

@JsonProperty("type")
private String type;
@JsonProperty("timestamp")
private Integer timestamp;
@JsonProperty("device")
private Integer device;
@JsonProperty("value")
private List<Value> value = null;

@JsonProperty("type")
public String getType() {
return type;
}

@JsonProperty("type")
public void setType(String type) {
this.type = type;
}

@JsonProperty("timestamp")
public Integer getTimestamp() {
return timestamp;
}

@JsonProperty("timestamp")
public void setTimestamp(Integer timestamp) {
this.timestamp = timestamp;
}

@JsonProperty("device")
public Integer getDevice() {
return device;
}

@JsonProperty("device")
public void setDevice(Integer device) {
this.device = device;
}

@JsonProperty("value")
public List<Value> getValue() {
return value;
}

@JsonProperty("value")
public void setValue(List<Value> value) {
this.value = value;
}

}
-----------------------------------com.example.Value.java-----------------------------------

package com.example;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"battery_level",
"var2",
"var3"
})
public class Value {

@JsonProperty("battery_level")
private Integer batteryLevel;
@JsonProperty("var2")
private Integer var2;
@JsonProperty("var3")
private Integer var3;

@JsonProperty("battery_level")
public Integer getBatteryLevel() {
return batteryLevel;
}

@JsonProperty("battery_level")
public void setBatteryLevel(Integer batteryLevel) {
this.batteryLevel = batteryLevel;
}

@JsonProperty("var2")
public Integer getVar2() {
return var2;
}

@JsonProperty("var2")
public void setVar2(Integer var2) {
this.var2 = var2;
}

@JsonProperty("var3")
public Integer getVar3() {
return var3;
}

@JsonProperty("var3")
public void setVar3(Integer var3) {
this.var3 = var3;
}

}

在 Java 对象和 JSON 就位后,使用 jackson 进行映射 -

ObjectMapper mapper = new ObjectMapper();
String jsonInString = "{'name' : 'mkyong'}";

//JSON from file to Object
User user = mapper.readValue(new File("c:\\user.json"), Example.class);

关于json - 如何在java中解析下面的Json文件,该文件没有 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58079858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com