JSON字符串,JSON对象的相互转换

JSON字符串,JSON对象的相互转换此处介绍几种 JSON 转换的几种方法 使用 GsonObjectMa 此为 Google 提供的一个转换工具添加依赖 使用谷歌的 Gson 转换依赖 dependency groupId com google code gson groupId artifactId gson artifactId dependency

此处介绍几种JSON转换的几种方法:使用Gson ObjectMapper 

Gson

此为Google提供的一个转换工具

添加依赖:

 
   
   
   
     com.google.code.gson 
    
   
     gson 
    
  

通过创建Gson对象进行调用方法转换

 User1 user1 = new User1(); user1.setId(1L); user1.setAge("18"); user1.setName("王大陆"); System.out.println("正常输出"+user1); Gson gson = new Gson(); String s = gson.toJson(user1); System.out.println("gson转换"+s); //得到结果:gson转换{"Id":1,"Name":"王大陆","age":"18"}

ObjectMapper

添加依赖:

 
   
   
   
     com.fasterxml.jackson.core 
    
   
     jackson-databind 
    
  

通过创建ObjectMapper进行Json的转换

基本上就是两种方法:

1.对象转换Json字符串:   writeValueAsString(Object obj);

将对象,List集合,Map集合转换成Json

2.Json字符串转换成对象: readValue(String json,Class clazz);

这个方法也不难,从json到简单POJO  或者 到 Map ,数组都没有问题;

 比如:readValue(json,Person.class);readValue(json,Map.class);readValue(json,Person[].class)

只是要注意json到list的转换有点特别的就可以

readValue(json,List.class); 该返回类型是:List >

所以,并不能通过readValue(json,List.class);来直接过得List

 @Test public void look() throws JsonProcessingException { //-------------------------------1.对象转Json----------------------------------- User1 user1 = new User1(); user1.setId(1L); user1.setAge("18"); user1.setName("王大陆"); //使用ObjectMapper进行转换 ObjectMapper objectMapper = new ObjectMapper(); //转换对象 String s1 = objectMapper.writeValueAsString(user1); System.out.println("ObjectMapper转换"+s1); //得到结果:ObjectMapper转换{"age":"18","name":"王大陆","id":1} //转换list集合 List 
  
    list1 = new ArrayList<>(); list1.add(new User1(1L,"lll","sss")); list1.add(new User1(2L,"llls","sssl")); String s2 = objectMapper.writeValueAsString(list1); System.out.println("list集合转换"+s2); //得到结果: // list集合转换[{"age":"sss","name":"lll","id":1},{"age":"sssl","name":"llls","id":2}] //转换Map HashMap map = new HashMap(); map.put("name","张三"); map.put("age",19); String s3 = objectMapper.writeValueAsString(map); System.out.println("map集合进行转换"+s3); //得到结果:map集合进行转换{"name":"张三","age":19} //-------------------------------2.Json转对象----------------------------------- //2.1Json转换为对象 User1 user2 = new User1(1L,"王大陆","19"); String s4 = objectMapper.writeValueAsString(user2);//得到一个Json值 User1 user11 = objectMapper.readValue(s4, User1.class); System.out.println("Json转对象"+user11); //得到结果:Json转对象User1(Id=1, Name=王大陆, age=19) //2.2Json转换为Map Map map1 = objectMapper.readValue(s4, Map.class); System.out.println("Json转换成Map"+map1); //得到结果:Json转换成Map{age=19, id=1, name=王大陆} //2.3Json转换成List集合 User1 user3 = new User1(1L,"王大陆","19"); User1 user4 = new User1(2L,"王小陆","18"); List 
   
     l = new ArrayList<>(); l.add(user3); l.add(user4); String s5 = objectMapper.writeValueAsString(l); List list = objectMapper.readValue(s5, List.class); System.out.println("Json转换成list"+list); //得到结果:Json转换成list[{age=19, name=王大陆, id=1}, {age=18, name=王小陆, id=2}] System.out.println(list.get(0));//只能得到下标的整体对象参数,不能得到具体的 //使用此方法讲要转换的类型设置为所需要的对象类型,就是list的构造器参数类型设置为:User1 JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, User1.class); List 
    
      o = objectMapper.readValue(s5, javaType); System.out.println(o.get(0).getName());//结果为:王大陆 } 
     
    
  

当string类型的数字 进行转换时 要进行判断是否为null,如果为空则转换成0

 Double zyyPrice = Double.parseDouble(billingDetails.getAccountZyy() == null ? "0" : billingDetails.getAccountZyy());

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/225635.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月17日 上午8:52
下一篇 2026年3月17日 上午8:53


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号