本篇主要演示如何使用Jackson对List, Map和数组与JSON互相转换.

本篇主要演示如何使用Jackson对List, Map和数组与JSON互相转换.

Java代码   收藏代码
  1. package com.jingshou.jackson;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.ArrayList;  
  5. import java.util.Arrays;  
  6. import java.util.Date;  
  7. import java.util.HashMap;  
  8. import java.util.List;  
  9. import java.util.Map;  
  10.   
  11. import com.fasterxml.jackson.databind.ObjectMapper;  
  12. import com.jingshou.pojo.Student;  
  13.   
  14. public class JacksonTest2 {  
  15.   
  16.     public static void main(String[] args) throws IOException {  
  17.         Student student1 = new Student();    
  18.         student1.setId(5237);  
  19.         student1.setName("jingshou");  
  20.         student1.setBirthDay(new Date());  
  21.           
  22.         Student student3 = new Student();    
  23.         student3.setId(5117);    
  24.         student3.setName("saiya");    
  25.         student3.setBirthDay(new Date());    
  26.           
  27.         ObjectMapper mapper = new ObjectMapper();  
  28.             
  29.         //Convert between List and JSON  
  30.         List<Student> stuList = new ArrayList<Student>();  
  31.         stuList.add(student1);  
  32.         stuList.add(student3);  
  33.         String jsonfromList = mapper.writeValueAsString(stuList);  
  34.         System.out.println(jsonfromList);  
  35.           
  36.         //List Type is not required here.  
  37.         List stuList2 = mapper.readValue(jsonfromList, List.class);  
  38.         System.out.println(stuList2);      
  39.         System.out.println("************************************");  
  40.           
  41.         //Convert Map to JSON  
  42.         Map<String, Object> map = new HashMap<String, Object>();  
  43.         map.put("studentList", stuList);  
  44.         map.put("class""ClassName");  
  45.         String jsonfromMap =  mapper.writeValueAsString(map);  
  46.         System.out.println(jsonfromMap);  
  47.           
  48.         Map map2 =  mapper.readValue(jsonfromMap, Map.class);  
  49.         System.out.println(map2);  
  50.         System.out.println(map2.get("studentList"));      
  51.         System.out.println("************************************");     
  52.           
  53.         //Convert Array to JSON  
  54.         Student[] stuArr = {student1, student3};  
  55.         String jsonfromArr =  mapper.writeValueAsString(stuArr);  
  56.         System.out.println(jsonfromArr);   
  57.         Student[] stuArr2 =  mapper.readValue(jsonfromArr, Student[].class);  
  58.         System.out.println(Arrays.toString(stuArr2));  
  59.     }  
  60.   
  61. }  

 运行结果:

Text代码   收藏代码
  1. [{"id":5237,"name":"jingshou","birthDay":1389528275987},{"id":5117,"name":"saiya","birthDay":1389528275987}]  
  2. [{id=5237, name=jingshou, birthDay=1389528275987}, {id=5117, name=saiya, birthDay=1389528275987}]  
  3. ************************************  
  4. {"class":"ClassName","studentList":[{"id":5237,"name":"jingshou","birthDay":1389528275987},{"id":5117,"name":"saiya","birthDay":1389528275987}]}  
  5. {class=ClassName, studentList=[{id=5237, name=jingshou, birthDay=1389528275987}, {id=5117, name=saiya, birthDay=1389528275987}]}  
  6. [{id=5237, name=jingshou, birthDay=1389528275987}, {id=5117, name=saiya, birthDay=1389528275987}]  
  7. ************************************  
  8. [{"id":5237,"name":"jingshou","birthDay":1389528275987},{"id":5117,"name":"saiya","birthDay":1389528275987}]  
  9. [Student [birthDay=Sun Jan 12 20:04:35 CST 2014, id=5237, name=jingshou], Student [birthDay=Sun Jan 12 20:04:35 CST 2014, id=5117, name=saiya]]  

 

再举一例实际应用:

小米网站注册页面输入邮件地址后,服务器提交的Ajax请求是:

https://account.xiaomi.com/pass/user@externalIdBinded?externalId=9999999%40qq.com&type=EM

服务器的返回是: &&&START&&&{"result":"ok","description":"成功","data":{"userId":-1},"code":0}

我们可以尝试用Map去读取后面那一段JSON

Java代码   收藏代码
  1. package com.jingshou.jackson;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.Map;  
  5.   
  6. import com.fasterxml.jackson.core.JsonParseException;  
  7. import com.fasterxml.jackson.databind.JsonMappingException;  
  8. import com.fasterxml.jackson.databind.ObjectMapper;  
  9.   
  10. public class JacksonTest3 {  
  11.   
  12.     public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {  
  13.         String json = "{\"result\":\"ok\",\"description\":\"成功\",\"data\":{\"userId\":-1},\"code\":0}";  
  14.         ObjectMapper mapper = new ObjectMapper();  
  15.         Map map = mapper.readValue(json, Map.class);  
  16.           //输出 {result=ok, description=成功, data={userId=-1}, code=0}  
  17.         System.out.println(map);  
  18.           //输出{userId=-1}  
  19.         Map dataMap = (Map) map.get("data");  
  20.         System.out.println(dataMap);          
  21.     }  
  22.   
  23. }  

 

可见以Key-Value形式的JSON字符串,都可以直接使用Map成功读取出来

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值