How can I prevent gson from converting integers to doubles

前端 未结 6 676
再見小時候
再見小時候 2020-12-05 07:37

I\'ve got integers in my json, and I do not want gson to convert them to doubles. The following does not work:

@Test
public void keepsIntsAsIs(){
    String          


        
6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-05 08:08

    Use Jackson instead of Gson, It solves your problem:

    import com.fasterxml.jackson.databind.ObjectMapper;
    import java.io.IOException;
    import java.util.Map;
    
    public class JacksonMapExample1 {
    
    public static void main(String[] args) {
    
        ObjectMapper mapper = new ObjectMapper();
        String json = "{\"name\":\"mkyong\", \"age\":\"37\"}";
        try {
            // convert JSON string to Map
            Map map = mapper.readValue(json, Map.class);
            System.out.println(map);
    
        } catch (IOException e) {
            e.printStackTrace();
        }
    
    }
    }
    

提交回复
热议问题