Riskhan
Riskhan

Reputation: 4470

Parsing json value with gson return null

I tried the below code to generate json

        public void GenJson(){
            HostDetails hostDetails = new HostDetails();
            hostDetails.status = true;
            hostDetails.deviceAddedTime = "2018-02-07 05:44:21.196541";
            hostDetails.hostname = "MyHost";
            hostDetails.id = 1;

            Gson gson = new GsonBuilder().serializeNulls().create();
            JsonElement je = gson.toJsonTree(hostDetails);
            JsonObject jo = new JsonObject();
            jo.add(hostDetails.getClass().getSimpleName(), je);
            String jstr = jo.toString();
            System.out.println(jstr);
        }

        class HostDetails {
            public boolean status;
            public String deviceAddedTime;
            public String hostname;
            public int id;
        }

Generated Json output is

{
    "HostDetails":{
        "deviceAddedTime":"2018-02-07 05:44:21.196541",
        "hostname":"MyHost",
        "status":true,
        "id":1
    }
}

I want to convert above json data to class using Gson. Below code i tried. While debugging member of hostDetails1 class returns null. How can i solve

public void GenClass(){
    Gson gson1 = new GsonBuilder().serializeNulls().create();
    HostDetails hostDetails1 = gson1.fromJson(jstr, HostDetails.class);
}

Upvotes: 0

Views: 134

Answers (2)

OneCricketeer
OneCricketeer

Reputation: 191963

This line is not necessary.

jo.add(hostDetails.getClass().getSimpleName(), je);

Neither is the jo variable.

You added an extra level to the JSON, where you can just create the JSON string right away in one line...

If you want to generate the correct JSON string, try

 Gson gson = new GsonBuilder().serializeNulls().create();

 String jstr = gson.toJSON(hostDetails);
 System.out.println(jstr);

Reasons

With this line

gson1.fromJson(jstr, HostDetails.class);

Gson is expecting this

{
    "deviceAddedTime":"2018-02-07 05:44:21.196541",
    "hostname":"MyHost",
    "status":true,
    "id":1
}

If you want to parse the given JSON, you need another wrapper class

class Foo {
    public Foo() {}

    @SerializedName("HostDetails")
    public HostDetails hostDetails;
}

Then

HostDetails hostDetails1  = gson1.fromJson(jstr, Foo.class).hostDetails;

And you need a default no-arg constructor

    class HostDetails {

        public HostDetails() {}

        public boolean status;
        public String deviceAddedTime;
        public String hostname;
        public int id;
    }

Maybe some setters and getters, too.

Upvotes: 3

Atif AbbAsi
Atif AbbAsi

Reputation: 6047

Try getter/setters ,and this approach .!

 class HostResponse{
     @SerializedName("HostDetails")
     HostDetails  hostDetails;
    }


class HostDetails {
            public boolean status;
            public String deviceAddedTime;
            public String hostname;
            public int id;
        }


 HostResponse hostResponse= new Gson().fromJson(json.toString(), HostResponse.class);

Upvotes: 0

Related Questions