一 操作案例
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<String,Object> rMap= new ObjectMapper().readValue(json, new TypeReference<Map<String,Object>>() {});
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}

本文通过实例展示了如何使用Java的Jackson和Fastjson库进行JSON字符串与Map对象之间的转换,包括处理空值和字段名引用。
2071

被折叠的 条评论
为什么被折叠?



