0

I'm trying to deserialize a JSON with GSON... Below I have the work I've done thus far... I'm able to deserialize the first layer thus far but nothing futher.

AsyncTask:

            //Deserialization
            Type deserializationType = new TypeToken<ReceiveMessageResponceModel<IncMessages>>() {}.getType();
            ReceiveMessageResponceModel<IncMessages> responceModel = gson.fromJson(sb.toString(), deserializationType);
            IncMessages messages = (IncMessages) responceModel.getValue();

            success = responceModel.getSuccess();

            Log.i(Utils.TAG, "getMessage: "+ responceModel.getMessage());

            if (success) {
                Log.i(Utils.TAG, "Name: "+ messages.getName());

                List<Messages> lstMessages = messages.getMessages();

                if (lstMessages != null) {
                    Log.i(Utils.TAG, "" + lstMessages.size());
                } else {
                    Log.i(Utils.TAG, "lstMessages is null");
                }
            }

ReceiveMessageResponceModel :

public class ReceiveMessageResponceModel <T>  {
    private Boolean success;
    private String message;
    private T data;

    public T getValue() {
        return data;
    }

    public void setValue(T data) {
        this.data = data;
    }

    public Boolean getSuccess() {
        return success;
    }

    public String getMessage() {
        return message;
    }
}

IncMessages :

public class IncMessages {

    String names;
    List<Messages> messages;

    public String getName() {
        return names;
    }

    public List<Messages> getMessages() {
        return messages;
    }
}

Messages:

public class Messages {
    String message_id;
    String user_id;
    String from_id;
    String group_id;
    String message_content;
    String message_extra;
    String message_type;
    String flag_status;
    String lock_status;
    String read_status;
    String del_status;
    String is_downloaded;
    String scheduled;
    String date_added;
    String date_scheduled;
    String to_avatar;
    String to_user;
    String from_avatar;
    String from_user;
    String from_me;
    ....

JSON:

{"success":true,"message":"messages successfully retrieved",
        "data":{
            "7148bc5e5065d61bd3a4b00318824db0":{
                "names":"Jason%20Caruso6",
                    "messages":[
                        {"message_id":"1953",
                            "user_id":"22fdaf2ad15eb23f62c14f1f9ef55f89",
                            "from_id":"22fdaf2ad15eb23f62c14f1f9ef55f89",
                            "group_id":"7148bc5e5065d61bd3a4b00318824db0",
                            "message_content":"blah!",
                            "message_extra":"",
                            "message_type":"plain",
                            "flag_status":"0",
                            "lock_status":"0",
                            "read_status":"0",
                            "del_status":"0",
                            "is_downloaded":"0",
                            "scheduled":"0",
                            "date_added":"2014-11-29 23:15:09",
                            "date_scheduled":"0000-00-00 00:00:00",
                            "to_avatar":"http:\/\/messageproapp.com\/_app\/_profiles\/e39933b7acd117ada0f3117c6a78a69c.jpg",
                            "to_user":"Jason%20Caruso6",
                            "from_avatar":"http:\/\/messageproapp.com\/_app\/_profiles\/e39933b7acd117ada0f3117c6a78a69c.jpg",
                            "from_user":"Jason%20Caruso6",
                            "from_me":true}]}}}

Logs:

11-30 01:44:49.241: I/PROJECTCARUSO(7488): getMessage: messages successfully retrieved
11-30 01:44:49.241: I/PROJECTCARUSO(7488): Name: null
11-30 01:44:49.241: I/PROJECTCARUSO(7488): lstMessages is null
jcaruso
  • 2,364
  • 1
  • 31
  • 64

2 Answers2

1

It seems like there is another level. You didn't take in account that there is the key "7148bc5e5065d61bd3a4b00318824db0" which maps your IncMessages object.

So something like this work:

class ReceiveMessageResponceModel <T>  {
    private Boolean success;
    private String message;
    private Data<T> data;

    public T getValue() {
        return data.t;
    }

    public void setValue(Data<T> data) {
        this.data = data;
    }

    public Boolean getSuccess() {
        return success;
    }

    public String getMessage() {
        return message;
    }
}


class Data<T> {    
   @SerializedName("7148bc5e5065d61bd3a4b00318824db0")
   T t;
}

class IncMessages {

    String names;
    List<Messages> messages;

    public String getName() {
        return names;
    }

    public List<Messages> getMessages() {
        return messages;
    }
}

Also I suppose that the key "7148bc5e5065d61bd3a4b00318824db0" is not constant. If you want to skip the key (or at least ignoring its real value), it can certainly be done with a custom deserializer. See also Dealing with randomly generated and inconsistent JSON field/key names using GSON

Community
  • 1
  • 1
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
  • I attempted that answer but it doesn't handle JSONArrays – jcaruso Dec 01 '14 at 03:38
  • @ZouZou I also have similar question [here](http://stackoverflow.com/questions/27329227/how-to-serialize-json-to-another-json-by-preserving-data-types-of-key) related to GSON. See if you can help me out? – john Dec 06 '14 at 16:56
0

I think your json object is not correctly formed. If I see your json string, Status is the key and true is the value, Message is the key and its corresponding value, Data is the key and IncMessage is value.

But in your json, what is 7148bc5e5065d61bd3a4b00318824db0 is it the value of data?

Try with this json

{
    "success": true,
    "message": "messages successfully retrieved",
    "data": {
        "names": "Jason%20Caruso6",
        "messages": [
            {
                "message_id": "1953",
                "user_id": "22fdaf2ad15eb23f62c14f1f9ef55f89",
                "from_id": "22fdaf2ad15eb23f62c14f1f9ef55f89",
                "group_id": "7148bc5e5065d61bd3a4b00318824db0",
                "message_content": "blah!",
                "message_extra": "",
                "message_type": "plain",
                "flag_status": "0",
                "lock_status": "0",
                "read_status": "0",
                "del_status": "0",
                "is_downloaded": "0",
                "scheduled": "0",
                "date_added": "2014-11-29 23:15:09",
                "date_scheduled": "0000-00-00 00:00:00",
                "to_avatar": "http://messageproapp.com/_app/_profiles/e39933b7acd117ada0f3117c6a78a69c.jpg",
                "to_user": "Jason%20Caruso6",
                "from_avatar": "http://messageproapp.com/_app/_profiles/e39933b7acd117ada0f3117c6a78a69c.jpg",
                "from_user": "Jason%20Caruso6",
                "from_me": true
            }
        ]
    }
}
Fluffeh
  • 33,228
  • 16
  • 67
  • 80
  • The JSON is correct that is not the issue but thanks for your input. The value, `7148bc5e5065d61bd3a4b00318824db0` , is `Jason%20Caruso6` user id. – jcaruso Dec 01 '14 at 00:47