Gson String to Object
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Gson String to Object相关的知识,希望对你有一定的参考价值。
我坚持如何解决这个问题,我想我理解的问题是gson的格式不正确,例如在这种情况下缺少Bsid。我如何解决它,所以如果这个问题,所以它不会再发生?
Error:E/androidRuntime: FATAL EXCEPTION: main
Process: , PID: 32171 com.google.gson.JsonSyntaxException: java.io.EOFException: End of input at line 1 column 65536 path $[555].bBsid
正在使用的代码:
if(json.length()== Integer.MAX_VALUE){
Log.d(TAG, "Error");
return null;
} else {
Log.i(TAG, json);
Type arrayType = new TypeToken<List<WifiObject>>() {
}.getType();
Gson gson = new Gson();
ArrayList<WifiObject> wifiObjectArrayList = new ArrayList<WifiObject>();
List wifiObjectList = gson.fromJson(json, arrayType);
for (int i = 0; i < wifiObjectList.size() - 1; i++) {
if (wifiObjectList.get(i) != null) {
WifiObject wifiObject = (WifiObject) wifiObjectList.get(i);
}
// Log.i(TAG, wifiObject.getBBSID());
// wifiObjectArrayList.add(wifiObject);
}
return wifiObjectArrayList;
}
宾语:
public WifiObject(String bBSID, String sSID, String capabilites, Integer frequency, Integer level){
//Instead of this.bssid for example because the setters already have Log's in them
setBBSID(bBSID);
setSSID(sSID);
setCapabilites(capabilites);
setFrequency(Integer.toString(frequency));
setLevel(Integer.toString(level));
}
试图转换这个:
02-28 23:57:53.876 4259-4259/ I/WifiHandler:
[{"bBsid":"00:62:ec:fd:ea:f0","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2462","level":"-45","sSid":"eduroam"},{"bBsid":"00:62:ec:fd:ea:f1","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2462","level":"-46","sSid":"KINGSWAP"},{"bBsid":"00:62:ec:fd:ea:f2","capabilities":"[ESS]","frequency":"2462","level":"-46","sSid":"The Cloud"},{"bBsid":"00:62:ec:fd:ea:f3","capabilities":"[WPA2-EAP-CCMP][WPA-EAP-TKIP][ESS]","frequency":"2462","level":"-46","sSid":"PLOCAL"},{"bBsid":"00:62:ec:fd:ea:f4","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2462","level":"-46","sSid":"SLaMFT"},{"bBsid":"00:62:ec:fd:ea:50","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2437","level":"-53","sSid":"eduroam"},{"bBsid":"00:62:ec:fd:ea:ff","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"5520","level":"-53","sSid":"eduroam"},{"bBsid":"00:62:ec:fd:ea:fd","capabilities":"[ESS]","frequency":"5520","level":"-53","sSid":"The Cloud"},{"bBsid":"00:62:ec:fd:ea:53","capabilities":"[WPA2-EAP-CCMP][WPA-EAP-TKIP][ESS]","frequency":"2437","level":"-54","sSid":"PLOCAL"},{"bBsid":"00:62:ec:fd:ea:51","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2437","level":"-55","sSid":"KINGSWAP"},{"bBsid":"00:62:ec:fd:ea:52","capabilities":"[ESS]","frequency":"2437","level":"-55","sSid":"The Cloud"},{"bBsid":"00:62:ec:fd:ea:fb","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"5520","level":"-55","sSid":"SLaMFT"},{"bBsid":"00:62:ec:fd:ea:54","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2437","level":"-55","sSid":"SLaMFT"},{"bBsid":"00:62:ec:fd:ea:5d","capabilities":"[ESS]","frequency":"5240","level":"-56","sSid":"The Cloud"},{"bBsid":"00:62:ec:fd:ea:fe","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"5520","level":"-56","sSid":"KINGSWAP"},{"bBsid":"00:62:ec:fd:ea:5e","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"5240","level":"-57","sSid":"KINGSWAP"},{"bBsid":"00:62:ec:fd:ea:5c","capabilities":"[WPA2-EAP-CCMP][WPA-EAP-TKIP][ESS]","frequency":"5240","level":"-57","sSid":"PLOCAL"},{"bBsid":"00:62:ec:fd:ea:5f","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"5240","level":"-58","sSid":"eduroam"},{"bBsid":"00:62:ec:fd:ea:5b","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"5240","level":"-58","sSid":"SLaMFT"},{"bBsid":"00:62:ec:fd:ea:fc","capabilities":"[WPA2-EAP-CCMP][WPA-EAP-TKIP][ESS]","frequency":"5520","level":"-58","sSid":"PLOCAL"},{"bBsid":"00:62:ec:fd:ec:30","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2437","level":"-59","sSid":"eduroam"},{"bBsid":"00:62:ec:fd:ee:61","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2412","level":"-59","sSid":"KINGSWAP"},{"bBsid":"00:62:ec:fd:ee:62","capabilities":"[ESS]","frequency":"2412","level":"-59","sSid":"The Cloud"},{"bBsid":"00:62:ec:fd:ee:64","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2412","level":"-59","sSid":"SLaMFT"},{"bBsid":"00:62:ec:fd:ec:31","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2437","level":"-60","sSid":"KINGSWAP"},{"bBsid":"00:62:ec:fd:ee:60","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2412","level":"-60","sSid":"eduroam"},{"bBsid":"00:62:ec:fd:ee:63","capabilities":"[WPA2-EAP-CCMP][WPA-EAP-TKIP][ESS]","frequency":"2412","level":"-60","sSid":"PLOCAL"},{"bBsid":"00:62:ec:fd:ec:32","capabilities":"[ESS]","frequency":"2437","level":"-60","sSid":"The Cloud"},{"bBsid":"00:62:ec:fd:ec:34","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2437","level":"-61","sSid":"SLaMFT"},{"bBsid":"00:42:68:a6:a6:41","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2462","level":"-62","sSid":"KINGSWAP"},{"bBsid":"00:42:68:a6:a6:42","capabilities":"[ESS]","frequency":"2462","level":"-62","sSid":"The Cloud"},{"bBsid":"00:42:68:a6:a6:43","capabilities":"[WPA2-EAP-CCMP][WPA-EAP-TKIP][ESS]","frequency":"2462","level":"-62","sSid":"PLOCAL"},{"bBsid":"00:42:68:a6:a6:44","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2462","level":"-62","sSid":"SLaMFT"},{"bBsid":"00:42:68:a6:a6:40","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2462","level":"-62","sSid":"eduroam"},{"bBsid":"00:62:ec:fd:ec:33","capabilities":"[WPA2-EAP
让我在短期内澄清这一点;
Gson gson = new Gson();
String json ="[{"bBsid":"00:62:ec:fd:ea:f0","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2462","level":"-45","sSid":"eduroam"}]"; // Your Json String
if (json.equalsIgnoreCase("")) {
return null;
}
YourClassName obj = gson.fromJson(json, YourClassName.class);
return obj;
这里,obj是你的类对象,它被转换为类对象,打印它的日志并检查它。希望这会帮助你。
可能的原因是Json字符串被截断。截断可能只发生在Log
打印时,但我认为现在情况并非如此。
也就是说,如果你从Json中删除最后一个不完整的WifiObject
并添加关闭的]
,那么右反序列化,使用34个WifiObjects
。
您应该跟踪生成Json的位置,并查看它们之间发生的情况。我遇到了同样的问题,但是我记不起在我的情况下是什么原因(如果我发现它会回来)。
我怀疑有一些东西就像在SharedPreferences
中存储这个长字符串,但是你还没有为Json代和后来的操作提供代码,所以很难说更具体的内容。
以上是关于Gson String to Object的主要内容,如果未能解决你的问题,请参考以下文章
GSON:java.lang.IllegalStateException:应为 BEGIN_OBJECT 但为 STRING
com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:应为 BEGIN_OBJECT 但为 STRING
使用Gson将Object转String出现u003d 的原因
Vue报错:Uncaught TypeError: Cannot assign to read only property 'exports' of object 的解决方法(代码片段