Gson. Deserialize integers as integers and not as doubles

后端 未结 7 1098
我寻月下人不归
我寻月下人不归 2020-12-09 16:46

I have json object with arbitary values inside. And I want to deserialize it in a Map. Everything is ok except converting integers to a doubles. See example:



        
7条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-09 17:23

    JSON only has a single Number type and there is no way for the parser to automatically tell what type to convert it to.

    If you aren't going to use a strongly typed object graph, consider using the JsonElement types:

    JsonObject root = new Gson().fromJson(json, JsonObject.class);
    int num = root.getAsJsonObject("inner_obj").get("num").getAsInt();
    

提交回复
热议问题