A small pit containing float data in Jackson's transmission map

As a json processing library, Jackson is very convenient for processing arrays, lists, and maps. However, in the process of map parsing, a small pit is encountered, which is recorded below.
Suppose we define a map whose value is object, and pass a Long and Float data to it.

         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() ;



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

double d = 3.14;
float f = (float)d;
System.out.println(f);
The output is: 3.14;

float f = 127.1f;
double d = f;
System.out.println(d);
The output is: 127.0999984741211

可以先转成BigDecimal ,再转成double

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


以及jackson传listmaparray的方式 ,(转自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));  
}  

}
“`

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325901139&siteId=291194637