map转json,json转map且保留值为null的转换

一 操作案例

1.1 演示代码

1.代码

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;


      String json = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\",\"pc\":null}";
        //json转map
      Map rMap=  new ObjectMapper().readValue(json, new TypeReference>() {});
      for(String key:rMap.keySet()){
          System.out.println(key+"=="+rMap.get(key));
      }
      //map转json
      String result=  JSON.toJSONString(rMap, SerializerFeature.WriteMapNullValue,SerializerFeature.QuoteFieldNames);
      System.out.println("result:"+result);

2.结果

name==John
age==30
city==New York
pc==null
result:{"name":"John","age":30,"city":"New York","pc":null}

你可能感兴趣的:(springcloud3,springboot,json,前端,服务器)