當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
将json数据转换成实体对象 JSON格式转换 JSON实体
生活随笔
收集整理的這篇文章主要介紹了
将json数据转换成实体对象 JSON格式转换 JSON实体
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
將json數據轉換成實體對象 JSON格式轉換 JSON實體
使用fastjson轉化為json類型的數據??
| ??<dependency> ?? ??? ??? ? ? ?<groupId>com.alibaba</groupId> ?? ??? ??? ? ? ?<artifactId>fastjson</artifactId> ?? ??? ??? ? ? ?<version>1.2.47</version> ?? ??? ??? ?</dependency> |
1 對象形式的json
| ?? ?@RequestMapping("/EasyJsonToObject.action") ?? ?public void EasyJsonToObject(){ ?? ??? ? ?? ??? ?//(1) 創建一個對象并將 給對象賦值 然后使用fastjson轉化為json類型的數據 ?? ?? ??? ??? ?Student student = new ?Student(); ?? ??? ??? ??? ??? ?student.setId(UUID.randomUUID().toString().replaceAll("-", ""));//生成id ?? ??? ??? ??? ??? ?student.setName("張三"); ?? ??? ??? ??? ??? ?student.setPassword("122455"); ?? ??? ??? ?String studentJson =?JSON.toJSONString(student);//將對象轉json ?? ??? ??? ?System.out.println(studentJson); ?? ??? ??? ??? ? ?? ??? ??? ? ?? ??? ?//(2)再將對象轉化為 ?? ??? ?Student changeStudent =?JSON.parseObject(studentJson,Student.class); ?? ??? ?System.out.println(changeStudent); ?? ?} |
2.1.1 將對象轉化為json
需要的實體類格式:
轉換過程:
2.1.2 將json數據轉為對象
2.2 將list對象轉為json 然后將對象list數據
| @RequestMapping("/listJsonToObject.action") ?? ?public void listJsonToObject(){ ?? ??? ? ?? ??? ?//(1) 創建一個對象并將 給對象賦值 然后使用fastjson轉化為json類型的數據 ?? ?? ??? ??? ? ? List<Student> list= new ArrayList<Student>(); ?? ??? ??? ??? ?Student student = new ?Student(); ?? ??? ??? ??? ??? ?student.setId(UUID.randomUUID().toString().replaceAll("-", ""));//生成id ?? ??? ??? ??? ??? ?student.setName("劉瑩"); ?? ??? ??? ??? ??? ?student.setPassword("123456"); ?? ??? ??? ? ? Student student2 = new ?Student(); ?? ??? ??? ??? ??? ?student2.setId(UUID.randomUUID().toString().replaceAll("-", ""));//生成id ?? ??? ??? ??? ??? ?student2.setName("張曉琪"); ?? ??? ??? ??? ??? ?student2.setPassword("789456"); ?? ??? ??? ??? ??? ?list.add(student); ?? ??? ??? ??? ??? ?list.add(student2); ?? ??? ??? ?String studentJson = JSON.toJSONString(list);//將對象轉json ?? ??? ??? ?System.out.println(studentJson); ?? ??? ? ?? ??? ? ?? ??? ?//(2)再將對象轉化為 ?? ??? ?List<Student> list2 = JSON.parseArray(studentJson,Student.class); ?? ??? ?System.out.println(list2); ?? ?} |
需要的實體類對象
2.3 ?對象中含有對象 轉化為對象
| ?? ?@RequestMapping("/mapListJsonToObject.action") ?? ?public void mapListJsonToObject(){ ?? ??? ? ?? ??? ?//(1) 創建一個對象并將 給對象賦值 然后使用fastjson轉化為json類型的數據 ?? ?? ??? ?HashMap<String, Object> map = new ? HashMap<String,Object>();//用來裝數據 ?? ??? ? ?? ??? ?Student student = new ?Student(); ?? ??? ??? ?student.setId(UUID.randomUUID().toString().replaceAll("-", ""));//生成id ?? ??? ??? ?student.setName("劉思佳"); ?? ??? ??? ?student.setPassword("123456"); ?? ??? ? ?? ??? ??? ?map.put("code", "200"); ?? ??? ??? ?map.put("data", student); ?? ??? ?String studentJson = JSON.toJSONString(map);//將對象轉json ?? ??? ?System.out.println(studentJson); ?? ??? ?//(2)再將對象轉化為 ?? ??? ?Data data = JSON.parseObject(studentJson,Data.class); ?? ??? ?System.out.println(data); ?? ?} |
2.4 對象中含有list的json格式
| ?? ?/** ?? ? * 3.復雜json轉為對象 ?? ? * ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 對象中包含數組的json ?? ? * http://localhost:8080/mavenssm20180519//josnIncludeListJsonToObject.action ?? ? *?@Title: josnIncludeListJsonToObject ?? ? * @Description:? ?? ? * @return void ?? ? * @throws? ?? ? ? @date 2018年7月22日 下午10:46:00 ?? ? */ ?? ?@RequestMapping("/josnIncludeListJsonToObject.action") ?? ?public void josnIncludeListJsonToObject(){ ?? ??? ?//(1) 創建一個對象并將 給對象賦值 然后使用fastjson轉化為json類型的數據 ?? ?? ??? ?HashMap<String, Object> map = new ? HashMap<String,Object>();//用來裝數據 ?? ??? ? ?? ??? ?List<Student> list= new ArrayList<Student>(); ?? ??? ?Student student = new ?Student(); ?? ??? ??? ?student.setId(UUID.randomUUID().toString().replaceAll("-", ""));//生成id ?? ??? ??? ?student.setName("劉思佳"); ?? ??? ??? ?student.setPassword("123456"); ?? ??? ?Student student2 = new ?Student(); ?? ??? ??? ?student2.setId(UUID.randomUUID().toString().replaceAll("-", ""));//生成id ?? ??? ??? ?student2.setName("陳曉瑩"); ?? ??? ??? ?student2.setPassword("789456"); ?? ??? ?list.add(student); ?? ??? ?list.add(student2); ?? ??? ??? ?map.put("code", "200"); ?? ??? ??? ?map.put("data", list); ?? ??? ??? ?String studentJson = JSON.toJSONString(map);//將對象轉json ?? ??? ?System.out.println(studentJson); ?? ??? ?//(2)再將對象轉化為 ?? ? Data data = JSON.parseObject(studentJson,Data.class); ?? ??? ?System.out.println(data); ?? ?} |
需要的實體類(****重要)
?將
總結
以上是生活随笔為你收集整理的将json数据转换成实体对象 JSON格式转换 JSON实体的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android app原型设计工具,手机
- 下一篇: JSON格式转换对象和字符串的转换