在Map中添加了Person对象,
Map<String, Object> = new HashMap<>();
但是在经过JSON.toJSONString()和JSON.parseObject(),会将Map中的Person实例解析成Map。
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Main {
private static Logger logger = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) throws Exception {
Map<String, Object> map = new HashMap<>();
map.put("1", new Person("张三", 21, "男"));
System.out.println(map);
var parseMap = JSON.parseObject(JSON.toJSONString(map), Map.class);
System.out.println(parseMap);
}
}
运行结果如下:
{1=Person(name=张三, age=21, gender=男)}
{1={"gender":"男","name":"张三","age":21}}
本文探讨在Java中如何处理Map与JSON交互时,如何避免将Person对象解析为Map,从而保留其原始属性。通过示例展示了可能的操作步骤和运行结果。
1763

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



