Maxim Shoustin
Maxim Shoustin

Reputation: 77904

Gson fails to convert String value to List, Java

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

Upvotes: 1

Views: 786

Answers (2)

Rahul
Rahul

Reputation: 45070

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.

Upvotes: 1

PeterMmm
PeterMmm

Reputation: 24630

Sould'nt this be in this form ? Pixel with 1 element:

{
"ClientData": {
    "pixelList":{
        "pixel": [{
            "b": 22,
            "a": 1234
        }]
    }
}
}

Upvotes: 2

Related Questions