Gson无法将字符串值转换为List,Java [英] Gson fails to convert String value to List, Java
问题描述
我有字符串,我需要将它转换为 目的。
以下是第一个的示例:
< pre $ {
ClientData:{
pixelList:{
pixel:[{
b:22,
a:1234
},
{
b:33,
a:34344
}]
}
看起来很简单,对吗?
所以我创建了后面的类,并成功地将上面提到的String转换为Java Object。
public class ClientDataRoot {
@SerializedName(ClientData)ClientData clientData = null;
}
public class ClientData {
PixelList pixelList = null;
}
public class PixelList {
List< Pixel>像素=空;
}
公共类像素{
private String a =;
private String b =;
}
从第一个例子你可以看到 pixelList
有2个对象: pixel
。
但是,如果您通过一个像素
获得 pixelList
会发生什么情况:
< :
{
ClientData:{
pixelList:{
pixel:{
b:22,
a:1234
}
}
}
$ / code>
在这里我得到这个错误:
com.google.gson.JsonParseException:JsonDeserializer
com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@d1918a未能反序列化
json object {b :22,a:1234}给定类型java.util.List< com.wefi.uxt.Pixel>
我的代码很简单:
import com.google.gson.Gson;
....
Gson gson = new Gson();
ClientDataRoot clientDataRoot1 =
gson.fromJson(xmlJSONObj.toString(),ClientDataRoot.class);
我使用 gson-1.7.1.jar
顺便说一句,如果我改变class PixelList
from
public class PixelList {
List< Pixel>像素=空;
到:
public class PixelList {
Pixel pixel = null;
}
它可以工作,但我需要它作为List
谢谢,
如何解决它:
正如大家所说,当我尝试将XML转换为JSON时,插件将
像素
转换为 JSONObjext
,而不是 JSONArray
。 以下是我解决问题的代码:
// responseBody是XML字符串
JSONObject xmlJSONObj = XML.toJSONObject(responseBody);
JSONObject cd =(JSONObject)xmlJSONObj.get(ClientData);
JSONObject pl =(JSONObject)cd.get(pixelList);
Object p = pl.get(pixel);
if(p instanceof JSONObject){
JSONArray arr = new JSONArray();
arr.put(p);
pl.remove(pixel);
pl.put(pixel,arr);
感谢PeterMmm和RJ
JSON $ c $ c>,或者如果你自己生成 JSON
,那么你做错了。 pixelList
中的一个单个元素应该有一个 JSON
,看起来像这样: - {
clientData:{
pixelList:{
pixel: [
{
a:22,
b:1234
}
]
}
}
}
或者JSON需要更正,否则您可以这样做。有两个对象,其中一个是 type1
(分析列表的一个),另一个是 type2
一个单一的元素json)。
现在使用 type1
类解析json并将它保存在 try-catch
。如果你得到这个 Exception
, catch
它并用 type2 $ c解析它$ c>类。这似乎是唯一的解决方法,如果你不能得到 JSON
响应修正。
I thought that Im strong enough to play with Gson but I found some problem.
I have String that I need to convert to Object. The String is generated from some xml.
Here is example of 1st one:
{
"ClientData": {
"pixelList": {
"pixel": [{
"b": 22,
"a": 1234
},
{
"b": 33,
"a": 34344
}]
}
}
}
Seems simple, right?
So I created followed classes and succeeded to convert above mentioned String to Java Object.
public class ClientDataRoot {
@SerializedName("ClientData") ClientData clientData = null;
}
public class ClientData {
PixelList pixelList = null;
}
public class PixelList {
List<Pixel> pixel = null;
}
public class Pixel {
private String a = "";
private String b = "";
}
From 1st example you can see that pixelList
has 2 objects: pixel
.
But what happens if you get pixelList
with one pixel
:
2nd one:
{
"ClientData": {
"pixelList": {
"pixel": {
"b": 22,
"a": 1234
}
}
}
}
Here I get the error:
com.google.gson.JsonParseException: The JsonDeserializer
com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@d1918a failed to deserialize
json object {"b":22,"a":1234} given the type java.util.List<com.wefi.uxt.Pixel>
My code is simple:
import com.google.gson.Gson;
....
Gson gson = new Gson();
ClientDataRoot clientDataRoot1 =
gson.fromJson(xmlJSONObj.toString(), ClientDataRoot.class);
I use gson-1.7.1.jar
BTW if I change class PixelList
from
public class PixelList {
List<Pixel> pixel = null;
}
to:
public class PixelList {
Pixel pixel = null;
}
It works, but I need it as List
Thank you,
How to Fix it:
As guys say when I try to convert XML to JSON, the plugin converts pixel
to JSONObjext
instead JSONArray
.
Here is a code I fixed my problem:
// responseBody is XML String
JSONObject xmlJSONObj = XML.toJSONObject(responseBody);
JSONObject cd = (JSONObject) xmlJSONObj.get("ClientData");
JSONObject pl = (JSONObject) cd.get("pixelList");
Object p = pl.get("pixel");
if (p instanceof JSONObject) {
JSONArray arr = new JSONArray();
arr.put(p);
pl.remove("pixel");
pl.put("pixel", arr);
}
Thanks to PeterMmm and R.J
解决方案 That is because either the JSON
returning third party is returning a wrong JSON
, or if you're generating the JSON
yourself, you're doing it wrongly. A single element in pixelList
should have a JSON
that would look like this:-
{
"clientData": {
"pixelList": {
"pixel": [
{
"a": "22",
"b": "1234"
}
]
}
}
}
Either the JSON needs to be corrected or else, you can do something like this. Have 2 objects, 1 of the type1
(one which parses a list) and the other of type2
(one which parses a single element json).
Now parse the json with type1
class and keep it in a try-catch
. If you get this Exception
, catch
it and parse it with the type2
class. This seems to be the only workaround, if you can't get the JSON
response fixed.
这篇关于Gson无法将字符串值转换为List,Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!