日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Jackson学习二之集合类对象与JSON互相转化--转载

發(fā)布時間:2025/4/5 javascript 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Jackson学习二之集合类对象与JSON互相转化--转载 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

原文地址:http://lijingshou.iteye.com/blog/2003059

本篇主要演示如何使用Jackson對List, Map和數(shù)組與JSON互相轉換.

package com.jingshou.jackson;import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map;import com.fasterxml.jackson.databind.ObjectMapper; import com.jingshou.pojo.Student;public class JacksonTest2 {public static void main(String[] args) throws IOException {Student student1 = new Student(); student1.setId(5237);student1.setName("jingshou");student1.setBirthDay(new Date());Student student3 = new Student(); student3.setId(5117); student3.setName("saiya"); student3.setBirthDay(new Date()); ObjectMapper mapper = new ObjectMapper();//Convert between List and JSONList<Student> stuList = new ArrayList<Student>();stuList.add(student1);stuList.add(student3);String jsonfromList = mapper.writeValueAsString(stuList);System.out.println(jsonfromList);//List Type is not required here.List stuList2 = mapper.readValue(jsonfromList, List.class);System.out.println(stuList2); System.out.println("************************************");//Convert Map to JSONMap<String, Object> map = new HashMap<String, Object>();map.put("studentList", stuList);map.put("class", "ClassName");String jsonfromMap = mapper.writeValueAsString(map);System.out.println(jsonfromMap);Map map2 = mapper.readValue(jsonfromMap, Map.class);System.out.println(map2);System.out.println(map2.get("studentList")); System.out.println("************************************"); //Convert Array to JSONStudent[] stuArr = {student1, student3};String jsonfromArr = mapper.writeValueAsString(stuArr);System.out.println(jsonfromArr); Student[] stuArr2 = mapper.readValue(jsonfromArr, Student[].class);System.out.println(Arrays.toString(stuArr2));}}

運行結果:

[{"id":5237,"name":"jingshou","birthDay":1389528275987},{"id":5117,"name":"saiya","birthDay":1389528275987}] [{id=5237, name=jingshou, birthDay=1389528275987}, {id=5117, name=saiya, birthDay=1389528275987}] ************************************ {"class":"ClassName","studentList":[{"id":5237,"name":"jingshou","birthDay":1389528275987},{"id":5117,"name":"saiya","birthDay":1389528275987}]} {class=ClassName, studentList=[{id=5237, name=jingshou, birthDay=1389528275987}, {id=5117, name=saiya, birthDay=1389528275987}]} [{id=5237, name=jingshou, birthDay=1389528275987}, {id=5117, name=saiya, birthDay=1389528275987}] ************************************ [{"id":5237,"name":"jingshou","birthDay":1389528275987},{"id":5117,"name":"saiya","birthDay":1389528275987}] [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]]

再舉一例實際應用:

小米網(wǎng)站注冊頁面輸入郵件地址后,服務器提交的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

package com.jingshou.jackson;import java.io.IOException; import java.util.Map;import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper;public class JacksonTest3 {public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {String json = "{\"result\":\"ok\",\"description\":\"成功\",\"data\":{\"userId\":-1},\"code\":0}";ObjectMapper mapper = new ObjectMapper();Map map = mapper.readValue(json, Map.class);//輸出 {result=ok, description=成功, data={userId=-1}, code=0} System.out.println(map);//輸出{userId=-1}Map dataMap = (Map) map.get("data");System.out.println(dataMap); }}

?

轉載于:https://www.cnblogs.com/davidwang456/p/4588925.html

總結

以上是生活随笔為你收集整理的Jackson学习二之集合类对象与JSON互相转化--转载的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。