JSON getting nested in a POJO

后端 未结 2 1434
太阳男子
太阳男子 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: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() {
                @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"}

提交回复
热议问题