[英]Deserialize Java array/list using gson
我在 Java 中使用 gson 來反序列化來自 Azure EventGrid 的一些 JSON(基本上它只是常規的 JSON 消息)。 我不斷收到為malmformed JSON
錯誤的malmformed JSON
拋出的異常,或者只是奇怪的錯誤,例如unterminated object at line 1 column 13 path $[0].msg
malmformed JSON
unterminated object at line 1 column 13 path $[0].msg
這是我試圖反序列化的 JSON 的樣子:
{
"id": "3984520-582350923-52389532042802",
"subject": "/v1-1/URL_PATH_HERE",
"data": [
{
"msg": "randomMsgHere",
"time": 1599922804852,
"type": "",
"alarm": true,
"source": "",
"category": "",
"severity": "",
"elementId": "",
"networkId": "",
"hardwareType": "",
"failureObject": "",
"reportingAgent": "",
"sourceDisplayName": "",
"failureObjectDisplayName": "",
"managedObjectDisplayName": "",
"extendedAttributes": {
"eventId": 100,
"customerAlias": "string",
"latitude": 10.00,
"longitude": 10.00,
"elevation": 10.00
}
}
],
"eventType": "eventType",
"dataVersion": "1",
"metadataVersion": "1",
"eventTime": "2020-10-13T01:41:32.765Z",
"topic": "randomTopicHere"
}
我只是在那里放了一些隨機數據(假裝空字符串有數據),我正在調用一個方法來提取或 get() JSON 的data [ ]屬性,所以我應該得到一個數組。 我已經有一個用 Java 定義的數據類,所以我可以輕松地反序列化它,但我通常使用來自 Jackson 的 Kotlin 數據類和對象映射器,我認為這個數組有問題。
將此 JSON 字符串轉換為這樣定義的EventState.class
的最簡單方法是什么?
@Getter
@Setter
public class EventState {
private String msg;
private long time;
private String type;
private boolean alarm;
private String source;
private String category;
private String severity;
private String elementId;
private String networkId;
private String hardwareType;
private String failureObject;
private String reportingAgent;
private String sourceDisplayName;
private String failureObjectDisplayName;
private String managedObjectDisplayName;
private ExtendedAttributes extendedAttributes;
@Getter
@Setter
public static class ExtendedAttributes {
private int eventId;
private String customerAlias;
private Double latitude;
private Double longitude;
private Double elevation;
}
上面的 json 字符串很容易通過 gson 解析,只需在 Eventstate 周圍添加一個容器。
下面是一個例子:
public class Test {
public static void main(String[] args) {
String value = "{\n" +
" \"id\": \"3984520-582350923-52389532042802\",\n" +
" \"subject\": \"/v1-1/URL_PATH_HERE\",\n" +
" \"data\": [\n" +
" {\n" +
" \"msg\": \"randomMsgHere\",\n" +
" \"time\": 1599922804852,\n" +
" \"type\": \"\",\n" +
" \"alarm\": true,\n" +
" \"source\": \"\",\n" +
" \"category\": \"\",\n" +
" \"severity\": \"\",\n" +
" \"elementId\": \"\",\n" +
" \"networkId\": \"\",\n" +
" \"hardwareType\": \"\",\n" +
" \"failureObject\": \"\",\n" +
" \"reportingAgent\": \"\",\n" +
" \"sourceDisplayName\": \"\",\n" +
" \"failureObjectDisplayName\": \"\",\n" +
" \"managedObjectDisplayName\": \"\",\n" +
" \"extendedAttributes\": {\n" +
" \"eventId\": 100,\n" +
" \"customerAlias\": \"string\",\n" +
" \"latitude\": 10.00,\n" +
" \"longitude\": 10.00,\n" +
" \"elevation\": 10.00\n" +
" }\n" +
"}\n" +
" ],\n" +
" \"eventType\": \"eventType\",\n" +
" \"dataVersion\": \"1\",\n" +
" \"metadataVersion\": \"1\",\n" +
" \"eventTime\": \"2020-10-13T01:41:32.765Z\",\n" +
" \"topic\": \"randomTopicHere\"\n" +
"}";
Gson gson = new Gson();
EventStateContainer eventStateContainer = gson.fromJson(value, EventStateContainer.class);
System.out.println(eventStateContainer.getData());
}
public class EventStateContainer {
private String id;
private String subject;
private ArrayList<EventState> data;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public ArrayList<EventState> getData() {
return data;
}
public void setData(ArrayList<EventState> data) {
this.data = data;
}
}
public class EventState {
private String msg;
private long time;
private String type;
private boolean alarm;
private String source;
private String category;
private String severity;
private String elementId;
private String networkId;
private String hardwareType;
private String failureObject;
private String reportingAgent;
private String sourceDisplayName;
private String failureObjectDisplayName;
private String managedObjectDisplayName;
private ExtendedAttributes extendedAttributes;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isAlarm() {
return alarm;
}
public void setAlarm(boolean alarm) {
this.alarm = alarm;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getSeverity() {
return severity;
}
public void setSeverity(String severity) {
this.severity = severity;
}
public String getElementId() {
return elementId;
}
public void setElementId(String elementId) {
this.elementId = elementId;
}
public String getNetworkId() {
return networkId;
}
public void setNetworkId(String networkId) {
this.networkId = networkId;
}
public String getHardwareType() {
return hardwareType;
}
public void setHardwareType(String hardwareType) {
this.hardwareType = hardwareType;
}
public String getFailureObject() {
return failureObject;
}
public void setFailureObject(String failureObject) {
this.failureObject = failureObject;
}
public String getReportingAgent() {
return reportingAgent;
}
public void setReportingAgent(String reportingAgent) {
this.reportingAgent = reportingAgent;
}
public String getSourceDisplayName() {
return sourceDisplayName;
}
public void setSourceDisplayName(String sourceDisplayName) {
this.sourceDisplayName = sourceDisplayName;
}
public String getFailureObjectDisplayName() {
return failureObjectDisplayName;
}
public void setFailureObjectDisplayName(String failureObjectDisplayName) {
this.failureObjectDisplayName = failureObjectDisplayName;
}
public String getManagedObjectDisplayName() {
return managedObjectDisplayName;
}
public void setManagedObjectDisplayName(String managedObjectDisplayName) {
this.managedObjectDisplayName = managedObjectDisplayName;
}
public ExtendedAttributes getExtendedAttributes() {
return extendedAttributes;
}
public void setExtendedAttributes(ExtendedAttributes extendedAttributes) {
this.extendedAttributes = extendedAttributes;
}
@Override
public String toString() {
return "EventState{" +
"msg='" + msg + '\'' +
", time=" + time +
", type='" + type + '\'' +
", alarm=" + alarm +
", source='" + source + '\'' +
", category='" + category + '\'' +
", severity='" + severity + '\'' +
", elementId='" + elementId + '\'' +
", networkId='" + networkId + '\'' +
", hardwareType='" + hardwareType + '\'' +
", failureObject='" + failureObject + '\'' +
", reportingAgent='" + reportingAgent + '\'' +
", sourceDisplayName='" + sourceDisplayName + '\'' +
", failureObjectDisplayName='" + failureObjectDisplayName + '\'' +
", managedObjectDisplayName='" + managedObjectDisplayName + '\'' +
", extendedAttributes=" + extendedAttributes +
'}';
}
}
public static class ExtendedAttributes {
private int eventId;
private String customerAlias;
private Double latitude;
private Double longitude;
private Double elevation;
public int getEventId() {
return eventId;
}
public void setEventId(int eventId) {
this.eventId = eventId;
}
public String getCustomerAlias() {
return customerAlias;
}
public void setCustomerAlias(String customerAlias) {
this.customerAlias = customerAlias;
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
public Double getElevation() {
return elevation;
}
public void setElevation(Double elevation) {
this.elevation = elevation;
}
@Override
public String toString() {
return "ExtendedAttributes{" +
"eventId=" + eventId +
", customerAlias='" + customerAlias + '\'' +
", latitude=" + latitude +
", longitude=" + longitude +
", elevation=" + elevation +
'}';
}
}
}
此致
托本
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.