3

I have a REST WCF Service that generate the following JSON.

{
   "RecordsUpdateResult":[
      {
         "ID":115,
         "Crud":2,
         "Data":{
            "__type":"Client:#PIT.Library.AndroidClasses",
            "ID":115,
            "Adress":"str. 1",
            "City":"Luzern",
            "Description":"Client 115",
            "PostalCode":6000
         }
      },
      {
         "ID":128,
         "Crud":2,
         "Data":{
            "__type":"Client:#PIT.Library.AndroidClasses",
            "ID":128,
            "Adress":"",
            "City":"",
            "Description":"Client 128",
            "PostalCode":0
         }
      }
   ]
}

How can I deserialize this on android phone with the gson library?

I suspect that the problem is the "__type":"Client:#PIT.Library.AndroidClasses". I have a class named "Client" on the Android side.

I would like to deserialize this JSON to the following class.

public class StammdatenUpdate extends ObjectBase
{
    @SerializedName("Crud")
    private CRUD    crud;
    @SerializedName("Data")
    private Object  data;

    @Override
    public String getSha1Hash()
    {
        // TODO Auto-generated method stub
        return null;
    }

    public synchronized CRUD getCrud()
    {
        return crud;
    }

    public synchronized void setCrud(CRUD crud)
    {
        this.crud = crud;
    }

    public synchronized Object getData()
    {
        return data;
    }

    public synchronized void setData(Object data)
    {
        this.data = data;
    }
}

The property ID is in the base class.

    ...
    Type listType = new TypeToken<ArrayList<StammdatenUpdate>>()
        {}.getType();
    Gson gson = new GsonBuilder().create();

    // List<StammdatenUpdate> RecordsUpdate(StammdatenType type, Dictionary<long, string> listWithHashes)
    postData.setParameter(new String[] { "type", "listWithHashes" });
    postData.setValues(new String[] { gson.toJson(StammdatenType.CLIENT), gson.toJson(map) });

    String json = post.execute(postData);

    List<StammdatenUpdate> retValue = null;
    if(Tools.isNullOrEmpty(json) == false)
    {

        retValue = gson.fromJson(json, listType);
    }

    return retValue;
KeepAlive
  • 227
  • 1
  • 17
GENiALi
  • 113
  • 1
  • 12
  • could the answer of this post be helpful for you? http://stackoverflow.com/questions/12197138/how-to-parse-inner-json-objects-arrays/12197186#12197186 – Mr.S Sep 25 '12 at 06:41
  • Not really. I use gson, my classes have the attributes @SerializedName. – GENiALi Sep 25 '12 at 06:52
  • The answer is also for gson, and the example also have the attributes @SerializedName... can you also post the class where you are making the request and handle the response? – Mr.S Sep 25 '12 at 07:03
  • I've updated the code sample. – GENiALi Sep 25 '12 at 07:28
  • Why don't you use the gson library for the json in the request? that would be much easier and you wouldn't have any problems with the deserialisation.. – Mr.S Sep 25 '12 at 07:37
  • What do you mean? I don't get it. – GENiALi Sep 25 '12 at 07:54
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/17104/discussion-between-mr-s-and-geniali) – Mr.S Sep 25 '12 at 08:26

1 Answers1

1

Change the WCF annotation from:

BodyStyle = WebMessageBodyStyle.Wrapped

to:

BodyStyle = WebMessageBodyStyle.WrappedRequest

This change the JSON from:

   {
       "RecordsUpdateResult":[
          {
             "ID":115,
             "Crud":2,
             "Data":{

to:

[
          {
             "ID":115,
             "Crud":2,
             "Data":{

And the deserialization works fine.

    Type listType = new TypeToken<ArrayList<StammdatenUpdate>>()
        {}.getType();
    Gson gson = new GsonBuilder().create();

    List<StammdatenUpdate> retValue = null;
    if(Tools.isNullOrEmpty(json) == false)
    {
        retValue = gson.fromJson(json, listType);
    }

    return retValue;
GENiALi
  • 113
  • 1
  • 12