JSON getting nested in a POJO

后端 未结 2 1432
太阳男子
太阳男子 2021-01-28 14:20

I have a POJO class as:

public class D{

    private JSONObject profileData;


    public JSONObject getProfileData ()
    {
        return profileData;
    }

         


        
相关标签:
2条回答
  • 2021-01-28 15:01

    You don't need to use Gson to convert hashmap to a json object. Simply use:

    profileDataInJson = new JSONObject(map);

    0 讨论(0)
  • 2021-01-28 15:09

    Add custom serializer to Gson, so that Gson serialize the org JSON as expected by you.

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(JSONObject.class, new JsonSerializer<JSONObject>() {
                @Override
                public JsonElement serialize(final JSONObject src, final Type typeOfSrc,
                        final JsonSerializationContext context) {
                    return new JsonParser().parse(src.toString()).getAsJsonObject();
                }
            });
    gsonBuilder.create().toJson(map1);
    

    This will return {"ioCinema":"firstValue","ioSIMAvailable":"firstKey","Name":"onePair"}

    0 讨论(0)
提交回复
热议问题