简体   繁体   中英

convert member field map in class into JSON by GSON

I have used GSON for a while, and I think it is quite useful!
Today I encounter a problem, and I think this maybe refer to some advanced usage of GSON .
here is my class definition:

class ResolvedID
{
    private String type;
    private HashMap<String,String> data=new HashMap<>(4);

    public String getType()
    {
        return this.type;
    }
    public void setType(String type)
    {
        this.type=type;
    }
    public void put(String K, String V)
    {
        this.data.put(K,V);
    }
}

after that I will do the following in some suitable way, here I just write in a simple way:
I will put some data into this hash map:

ResolvedID id=new ResolvedID();
id.setType("Bachelor");
id.put("school","Computer Science School");
id.put("major","Software Engineering");
id.put("grade","2009");

and I expect GSON might convert it into
{"type":"Bachelor", "data":{"school":"Computer Science School", "grade":"2009", "major":"Software Engineering"}}

But unfortunately I only got
{"type":"Bachelor"}}

Can anyone give some direction for these kind of member field container to convert into json by GSON

This

A a = new A();
a.data = new HashMap<>();
a.type="teacher";
a.data.put("degree","bachelor");
a.data.put("grade","2013");
Gson gson = new Gson();
System.out.println(gson.toJson(a));

prints

{"type":"teacher","data":{"degree":"bachelor","grade":"2013"}}

for me.

You must not be initializing data . Gson, by default, will not serialize a field if it is null .

It might be easier to use JavaBeans with nested classes. Here's an example:

Note: Obviously you'd build your JsonResponseData class in a different way - I just set prefixed values from within the class for a quick example.

Nested Pojo:

public class JsonResponseData {

    private JsonResponse mResponse = new JsonResponse();

    public JsonResponse getJsonResponse(){
        return mResponse;
    }

    public void setJsonResponse(JsonResponse mResponse){
        this.mResponse = mResponse;
    }

    private static class JsonResponse {
        private int part = 1;
        private UserData data = new UserData();
    }

    private static class UserData {
        private User user = new User();

        public User getUser(){
            return user;
        }

        public void setUser(User user){
            this.user = user;
        }
    }

    public static class User {
        private String id = "5";
        private String firstName = "User";
        private String lastName = "Name";

        public String getId(){
            return id;
        }

        public void setId(String id){
            this.id = id;
        }

        public String getFirstName(){
            return firstName;
        }

        public void setFirstName(String firstName){
            this.firstName = firstName;
        }

        public String getLastName(){
            return lastName;
        }

        public void setLastName(String lastName){
            this.lastName = lastName;
        }
    }

}

Test Class:

public class Test {

    private static Gson gson;
    private static JsonResponseData data;

    public static void main(String[] args){
        gson = new Gson();
        data = new JsonResponseData();

        System.out.print(gson.toJson(data));

    }

}

Output:

{"mResponse":{"part":1,"data":{"user":{"id":"5","firstName":"User","lastName":"Name"}}}}

I solve this problem at last, it is my problem that I have no getter for this hash map, I think GSON will use reflect the member field.

If member field is public scoped, GSON can get these field and serialize them.
If member field is private scoped, GSON will get them by getter , hence GSON could not serialize those private field that have no getter , like my instance above.

class ResolvedID
{
    private String type;
    private HashMap<String,String> data=new HashMap<>(4);

    public String getType()
    {
        return this.type;
    }
    public void setType(String type)
    {
        this.type=type;
    }
    //----getter is important for private member field to be reflected by GSON!
    public HashMap getData()
    {
        return this.data;
    }
    public void put(String K, String V)
    {
        this.data.put(K,V);
    }
}

After adding a getter for my class, it is now successfully executed! Now I get more comprehension with GSON 's reflection procedure!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM