美文网首页
Jackson传输map中含有float数据的一个小坑

Jackson传输map中含有float数据的一个小坑

作者: 安迪猪 | 来源:发表于2017-12-17 10:13 被阅读0次

    Jackson作为一个json的处理库,在处理数组,list,map方面非常便利。但是在进行map解析过程中,遇到一个小坑,下面进行记录。
    假设我们定义一个value是object的map,并向其中传入一个Long型和Float的数据.

             ObjectMapper mapper = new ObjectMapper();  
             Map<String, Object> map = new HashMap<String, Object>();  
             map.put("sessionID", 1L);
             map.put("temperatureValue", 60.15F);
    

    然后我们将map数据取出

          Long sessionID = ((Long) map.get("sessionID"));
          System.out.println("sessionID is " + sessionID);
          float temperatureValue = ((Float) map.get("temperatureValue"));
          System.out.println("temperatureValue is " + temperatureValue);
    

    结果没有任何问题:

          sessionID is 1
          temperatureValue is 60.15
    

    然后我们将此map数据进行Jaskson封装成字符串,然后进行恢复成map,再进行map数据的提取

            ObjectMapper mapper = new ObjectMapper();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("sessionID", 1L);
            map.put("temperatureValue", 60.15F);
            String jsonfromMap = mapper.writeValueAsString(map);
            System.out.println(jsonfromMap);
    
            Map map2 = mapper.readValue(jsonfromMap, Map.class);
    
            Long sessionID = ((Long) map.get("sessionID"));
            System.out.println("sessionID is " + sessionID);
    
            Float temperatureValue = (Float) map2.get("temperatureValue");
    
            System.out.println("temperatureValue-->" + temperatureValue);
    

    结果

    {"sessionID":1,"temperatureValue":60.15}
    sessionID is 1
    

    可以发现Float类型的temperatureValue 值没有解析出来。将Float改成Double,再进行解析

    Double temperatureValue = (Double) map2.get("temperatureValue");
    

    结果出来

    temperatureValue-->60.15
    

    然后再将Double转成Float。

    float temperatureValue4float=(float)temperatureValue.doubleValue() ;
    

    另附上float和double互相换砖的问题( 参考别人博客):

    double d = 3.14;  
    float f = (float)d;  
    System.out.println(f);  
    输出结果是:3.14;   
      
    float f = 127.1f;  
    double d = f;  
    System.out.println(d);  
    输出结果是:127.0999984741211   
    

    可以先转成BigDecimal ,再转成double

     f = 127.1f;  
            BigDecimal b = new BigDecimal(String.valueOf(f));  
             d = b.doubleValue();  
            System.out.println(d);  
    

    以及jackson传list,map和array的方式 ,(转自http://blog.csdn.net/lansetiankong12/article/details/52584104)

    package com.jingshou.jackson;  
      
    import java.io.IOException;  
    import java.util.ArrayList;  
    import java.util.Arrays;  
    import java.util.Date;  
    import java.util.HashMap;  
    import java.util.List;  
    import java.util.Map;  
      
    import com.fasterxml.jackson.databind.ObjectMapper;  
    import com.jingshou.pojo.Student;  
      
    public class JacksonTest2 {  
      
        public static void main(String[] args) throws IOException {  
            Student student1 = new Student();    
            student1.setId(5237);  
            student1.setName("jingshou");  
            student1.setBirthDay(new Date());  
              
            Student student3 = new Student();    
            student3.setId(5117);    
            student3.setName("saiya");    
            student3.setBirthDay(new Date());    
              
            ObjectMapper mapper = new ObjectMapper();  
                
            //Convert between List and JSON  
            List<Student> stuList = new ArrayList<Student>();  
            stuList.add(student1);  
            stuList.add(student3);  
            String jsonfromList = mapper.writeValueAsString(stuList);  
            System.out.println(jsonfromList);  
              
            //List Type is not required here.  
            List stuList2 = mapper.readValue(jsonfromList, List.class);  
            System.out.println(stuList2);      
            System.out.println("************************************");  
              
            //Convert Map to JSON  
            Map<String, Object> map = new HashMap<String, Object>();  
            map.put("studentList", stuList);  
            map.put("class", "ClassName");  
            String jsonfromMap =  mapper.writeValueAsString(map);  
            System.out.println(jsonfromMap);  
              
            Map map2 =  mapper.readValue(jsonfromMap, Map.class);  
            System.out.println(map2);  
            System.out.println(map2.get("studentList"));      
            System.out.println("************************************");     
              
            //Convert Array to JSON  
            Student[] stuArr = {student1, student3};  
            String jsonfromArr =  mapper.writeValueAsString(stuArr);  
            System.out.println(jsonfromArr);   
            Student[] stuArr2 =  mapper.readValue(jsonfromArr, Student[].class);  
            System.out.println(Arrays.toString(stuArr2));  
        }  
      
    } 
    

    相关文章

      网友评论

          本文标题:Jackson传输map中含有float数据的一个小坑

          本文链接:https://www.haomeiwen.com/subject/pasewxtx.html