gpt4 book ai didi

java - 为什么我收到 JsonSyntaxException : Expected BEGIN_ARRAY but was STRING here?

转载 作者:行者123 更新时间:2023-12-01 11:38:09 26 4
gpt4 key购买 nike

我有一个 JSON 文件:

{
"settings": {
"header": {
"id": "240A64CDC43F",
"filename": "network",
"sequence": "0",
"last_modified": "2015-04-21T16:33",
"JSON_file_version": "2.1"
},
"data": {
"engine_config": {
"bed_name": "IdealTempCDC43F",
"provisioned": "false",
"connected": "false",
"IP_address": "192.168.10.1",
"connection_error": "None"
},
"networks": {
"available": "7",
"SSIDs": {
"wireless_1": {
"SSID": "$$ASI_WIFI$$",
"mac_address": "A0:EC:F9:11:35:04",
"channel": "11",
"RSSI": "-64dBm",
"security": "true",
"security_type": "WPA"
},
"wireless_2": {
"SSID": "$$ASI_GUEST$$",
"mac_address": "A0:EC:F9:11:35:02",
"channel": "11",
"RSSI": "-65dBm",
"security": "true",
"security_type": "WPA"
}
}
}
}
}
}

为了解析这个 json,我使用这个:

HttpResponse response = client.execute(request); 
String strResponse = response.toString();
parseJSON(strResponse);

public void parseJSON(String jsonString) {
Gson gsonParser = new Gson();
ProveQuerySetting gsonResponse = new ProveQuerySetting();
Type collectionType = new TypeToken<Collection<ProveQuerySetting>>() {
}.getType();
gsonResponse = gsonParser.fromJson(jsonString, collectionType);
String str = gsonParser.toJson(gsonResponse);
System.out.println(" final json " + str);
}

但是我遇到了这个异常:

04-21 07:08:13.864: W/System.err(4187): com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $
04-21 07:08:13.864: W/System.err(4187): com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $
04-21 07:08:13.865: W/System.err(4187): at com.google.gson.Gson.fromJson(Gson.java:822)
04-21 07:08:13.865: W/System.err(4187): at com.google.gson.Gson.fromJson(Gson.java:775)
04-21 07:08:13.865: W/System.err(4187): at com.google.gson.Gson.fromJson(Gson.java:724)
04-21 07:08:13.865: W/System.err(4187): at com.easi.main.network.NetworkCom.parseJSON(NetworkCom.java:127)
04-21 07:08:13.865: W/System.err(4187): at com.easi.main.network.NetworkCom.sendFirst(NetworkCom.java:50)
04-21 07:08:13.865: W/System.err(4187): at com.easi.main.setting.ConnectToBox$MyAsyncTask.doInBackground(ConnectToBox.java:89)
04-21 07:08:13.865: W/System.err(4187): at com.easi.main.setting.ConnectToBox$MyAsyncTask.doInBackground(ConnectToBox.java:1)
04-21 07:08:13.865: W/System.err(4187): at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-21 07:08:13.865: W/System.err(4187): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-21 07:08:13.865: W/System.err(4187): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-21 07:08:13.865: W/System.err(4187): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-21 07:08:13.865: W/System.err(4187): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-21 07:08:13.865: W/System.err(4187): at java.lang.Thread.run(Thread.java:818)
04-21 07:08:13.865: W/System.err(4187): Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $
04-21 07:08:13.865: W/System.err(4187): at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350)
04-21 07:08:13.866: W/System.err(4187): at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:79)
04-21 07:08:13.866: W/System.err(4187): at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
04-21 07:08:13.866: W/System.err(4187): at com.google.gson.Gson.fromJson(Gson.java:810)
04-21 07:08:13.866: W/System.err(4187): ... 12 more

这是我的 POJO 类:

public class ProveQuerySetting {
ProvQueryData provEngnData;
ProvQueryHeader provEngnhHeader;

public ProvQueryData getProvEngnData() {
return provEngnData;
}

public void setProvEngnData(ProvQueryData provEngnData) {
this.provEngnData = provEngnData;
}

public ProvQueryHeader getProvEngnhHeader() {
return provEngnhHeader;
}

public void setProvEngnhHeader(ProvQueryHeader provEngnhHeader) {
this.provEngnhHeader = provEngnhHeader;
}
}

为什么我会收到此异常?

最佳答案

好的,所以您正在尝试使用默认的 Gson。伟大的!默认的Gson是智能的,但不是那么智能。您必须将类设计为完全匹配您返回的 json 格式,或者使用 @SerializedName 注释,如果您不想设计自定义反序列化器。当我说完全匹配时,我的意思是,字段名称必须与序列化的Gson完全相同,无论是在字段名称还是在注释中。

我创建了以下 POJO(包括您的 POJO,并修改了字段名称)。为了简洁起见,我没有包含 getter/setter:

public class ProvEngineConfig {
private String bed_name;
private String provisioned;
private String connected;
private String IP_Address;
private String connection_error;
}

public class ProvNetworks {
private String available;
private Map<String, ProvSSID> SSIDs; // Notice this is a map
}

public class ProveQuerySetting {
ProvQueryData data;
ProvQueryHeader header;
}

public class ProvQueryData {
ProvEngineConfig engine_config;
ProvNetworks networks;
}

public class ProvQueryHeader {
String id;
String filename;
String sequence;
String last_modified;
String JSON_file_version;
}

public class ProvSSID {
String SSID;
String mac_address;
String channel;
String RSSI;
String security;
String security_type;
}

再次,您是否看到字段名称与 JSON 中的字段名称完全相同?请注意,在 ProvNetworks 中,我使用的是 map ,因为您提供的 JSON 是无线名称与完全匹配的 SSID 对象的 map 。

一旦我的 POJO 设置如下,另请注意,您提供的 JSON 不是 Collection查询设置,它实际上是 Map<String, ProvQuerySetting> ,您从单词 "settings" 进行映射到引擎配置对象。所以我这样解析:

public static void parseJSON(String jsonString) {
Gson gsonParser = new Gson();
Map<String, ProveQuerySetting> gsonResponse; // You don't need to do new here
Type collectionType = new TypeToken<Map<String, ProveQuerySetting>>() {}.getType();
gsonResponse = gsonParser.fromJson(jsonString, collectionType);

String str = gsonParser.toJson(gsonResponse);
System.out.println(" final json " + str);
}

输出:

final json {"settings":{"data":{"engine_config":{"bed_name":"IdealTempCDC43F","provisioned":"false","connected":"false","connection_error":"None"},"networks":{"available":"7","SSIDs":{"wireless_1":{"SSID":"$$ASI_WIFI$$","mac_address":"A0:EC:F9:11:35:04","channel":"11","RSSI":"-64dBm","security":"true","security_type":"WPA"},"wireless_2":{"SSID":"$$ASI_GUEST$$","mac_address":"A0:EC:F9:11:35:02","channel":"11","RSSI":"-65dBm","security":"true","security_type":"WPA"}}}},"header":{"id":"240A64CDC43F","filename":"network","sequence":"0","last_modified":"2015-04-21T16:33","JSON_file_version":"2.1"}}}
<小时/>

总结:

您需要:

  1. 完全匹配您的 POJO 和字段名称/带注释的字段名称以匹配 JSON
  2. 编写您自己的自定义解串器,如 JsonReader 中所述。

关于java - 为什么我收到 JsonSyntaxException : Expected BEGIN_ARRAY but was STRING here?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29793314/

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