0

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);
}
1
  • @cricket_007 Please explain more Commented Feb 21, 2018 at 6:24

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I can't avoid root element "HostDetails" in json. So how can i create json with String jstr = gson.toJSON(hostDetails);
1) Why not? 2) You already created it. I showed you how to parse it.
gson.toJSON(object) will take the object to a string without the root element. github.com/google/gson/blob/master/UserGuide.md#object-examples
How to add root element to json with String jstr = gson.toJSON(hostDetails); line
You make hostDetails a completely separate object. Such as a hostDetails = new Foo() in the example code I provided.
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.