Gson fails to convert String value to List, Java

Morbius

New Member
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:\[code\]{"ClientData": { "pixelList": { "pixel": [{ "b": 22, "a": 1234 }, { "b": 33, "a": 34344 }] }}}\[/code\]Seems simple, right?So I created followed classes and succeeded to convert above mentioned String to Java Object.\[code\]public class ClientDataRoot { @SerializedName("ClientData") ClientData clientData = http://stackoverflow.com/questions/15784786/null; }public class ClientData { PixelList pixelList = null; }public class PixelList { List<Pixel> pixel = null;}public class Pixel { private String a =""; private String b = ""; \[/code\]}From 1st example you can see that \[code\]pixelList\[/code\] has 2 objects: \[code\]pixel\[/code\].But what happens if you get \[code\]pixelList\[/code\] with one \[code\]pixel\[/code\]:2nd one:\[code\]{"ClientData": { "pixelList": { "pixel": { "b": 22, "a": 1234 } }}}\[/code\]Here I get the error:\[code\]com.google.gson.JsonParseException: The JsonDeserializercom.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@d1918a failed to deserializejson object {"b":22,"a":1234} given the type java.util.List<com.wefi.uxt.Pixel>\[/code\]My code is simple:\[code\]import com.google.gson.Gson;....Gson gson = new Gson();ClientDataRoot clientDataRoot1 = gson.fromJson(xmlJSONObj.toString(), ClientDataRoot.class);\[/code\]I use \[code\]gson-1.7.1.jar\[/code\]BTW if I change class \[code\]PixelList\[/code\] from\[code\] public class PixelList { List<Pixel> pixel = null; }\[/code\]to:\[code\] public class PixelList { Pixel pixel = null; }\[/code\]It works, but I need it as ListThank you,
 
Back
Top