gpt4 book ai didi

java - 如何将我的 JSON 属性之一转换为数组?

转载 作者:行者123 更新时间:2023-12-02 11:17:31 25 4
gpt4 key购买 nike

如何将我的“威胁”转换为数组?

这是我的 JSON

[
{
"SensorType": "Visual",
"Latitude": 1.3184418,
"Longitude": 103.6282628,
"Threat": [
{
"ThreatId": 56332,
"Timestamp": "2018-05-02T13:15:43.6964862+08:00",
"Latitude": 0,
"Longitude": 0,
"Bearing": 22.0,
"FOV": 10.0,
"ObjectId": "5320079"
}
]
}
]

这是我的 Java 文件

public class Threat {
//public boolean IsNewDetection;
public String SystemId;
public String SystemName;
public String SensorType;
public Double Latitude;
public Double Longitude;
public ThreatTimeDetail Threat = new ThreatTimeDetail();

public class ThreatTimeDetail {
public Integer ThreatId;
public Date Timestamp;
public Double Latitude;
public Double Longitude;
public Float Bearing;
public Float FOV;
public String ObjectId;
}
public transient Date Timestamp;
public transient Date mTimestamp;
public transient boolean isUpdated;
}

这是我的请求威胁 Java 文件

bufferedReader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
if (bufferedReader != null) {

//not working
Threat[] Sensorarray = new GsonBuilder()
.create()
.fromJson(bufferedReader, Threat[].class);
}

The error state:Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 80 path $[0].Threat at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)

如果我从 JSON 文件中删除“Threat :”后面的 [],它就可以正常工作。

为什么会发生这种情况以及如何解决?

最佳答案

这里的问题是您正在尝试从 Json 转换数组:

[
{
... ,
"Threat": [ <<<<<<<<<<<<<
{
"ThreatId": 56332,
"Timestamp": "2018-05-02T13:15:43.6964862+08:00",
... ,
"ObjectId": "5320079"
}
] <<<<<<<<<<<<<<
}
]

进入对象:

public ThreatTimeDetail Threat = new ThreatTimeDetail();
<小时/>

解决方案是创建一个数组而不是简单的对象:

public ThreatTimeDetail[] Threat = new ThreatTimeDetail[50];

关于java - 如何将我的 JSON 属性之一转换为数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50170308/

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