Gson force use int instead of double

独自空忆成欢 提交于 2019-12-04 01:09:35

I think you could simply change your DataPacket class to

public class DataPacket {
  private String fn;
  private ChatPacket p; // will be a StringMap
}

(replace Object by ChatPacket) - if that suits into the rest of your application. Gson will then recognize the data types of the ChatPacket class and will trait the "1" as int.

EDIT (as you can't use full formatting features in comments :-( )

You can use a combination of Gson and it's low level helper JsonParser like this:

String testStr = "{fn: chat, data: {roomId: 1, message: \"Some brabble\"}}";
DataPacket dp = new Gson().fromJson(testStr, DataPacket.class); 
JsonParser parser = new JsonParser(); 
JsonElement e = parser.parse(testStr); 
ChatPacket cp = new Gson().fromJson(e.getAsJsonObject().getAsJsonObject("data"), ChatPacket.class);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!