实体类和JSON对象之间相互转化
. [代码]工具类
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
package myUtil;import java.io.IOException;import myProject.Student;import myProject.StudentList;import org.codehaus.jackson.map.ObjectMapper;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;/** * 实体类和JSON对象之间相互转化(依赖包jackson-all-1.7.6.jar、jsoup-1.5.2.jar) * @author wck * */public class JSONUtil { /** * 将json转化为实体POJO * @param jsonStr * @param obj * @return */ public static<T> Object JSONToObj(String jsonStr,Class<T> obj) { T t = null; try { ObjectMapper objectMapper = new ObjectMapper(); t = objectMapper.readValue(jsonStr, obj); } catch (Exception e) { e.printStackTrace(); } return t; } /** * 将实体POJO转化为JSON * @param obj * @return * @throws JSONException * @throws IOException */ public static<T> JSONObject objectToJson(T obj) throws JSONException, IOException { ObjectMapper mapper = new ObjectMapper(); // Convert object to JSON string String jsonStr = ""; try { jsonStr = mapper.writeValueAsString(obj); } catch (IOException e) { throw e; } return new JSONObject(jsonStr); } public static void main(String[] args) throws JSONException, IOException { JSONObject obj = null; obj = new JSONObject(); obj.put("name", "213"); obj.put("age", 27); JSONArray array = new JSONArray(); array.put(obj); obj = new JSONObject(); obj.put("name", "214"); obj.put("age", 28); array.put(obj); Student stu = (Student) JSONToObj(obj.toString(), Student.class); JSONObject objList = new JSONObject(); objList.put("student", array); System.out.println("objList:"+objList); StudentList stuList = (StudentList) JSONToObj(objList.toString(), StudentList.class); System.out.println("student:"+stu); System.out.println("stuList:"+stuList); System.out.println("#####################################"); JSONObject getObj = objectToJson(stu); System.out.println(getObj); }} |
2. [代码]实体(子类)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package myProject;public class Student { private String name; private int age; //private StudentClass studentClass; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + "]"; }} |
3. [代码]实体父类 跳至 [1] [2] [3] [全屏预览]
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package myProject;import java.util.List;public class StudentList { List<Student> student; public List<Student> getStudent() { return student; } public void setStudent(List<Student> student) { this.student = student; } @Override public String toString() { return "StudentList [student=" + student + "]"; } } |
实体类和JSON对象之间相互转化的更多相关文章
- 使用FastJson对实体类和Json还有JSONObject之间的转换
1. 实体类或集合转JSON串 String jsonString = JSONObject.toJSONString(实体类); 2.JSON串转JSONObject JSONObject json ...
- .net 实体类与json转换(.net自带类库实现)更新
上一篇文章中写到在.net中实体类跟json格式的相互转换,今天在做具体转换时候,发现之前版本的jsonhelp对于日期类型的转换不全面.之前版本的jsonhelp中从实体类转换成json格式时候,将 ...
- .net 实体类与json转换(.net自带类库实现)
注意要点. 1.jsonhelp编写时候添加的引用.System.Runtime.Serialization.Json; 2.实体类需声明为public jsonhelp代码: using Syste ...
- 实体类和json互相转换
/// <summary> /// 将实体类转换为json数据 /// </summary> /// <returns></returns> publi ...
- ASP.NET实现二维码 ASP.Net上传文件 SQL基础语法 C# 动态创建数据库三(MySQL) Net Core 实现谷歌翻译ApI 免费版 C#发布和调试WebService ajax调用WebService实现数据库操作 C# 实体类转json数据过滤掉字段为null的字段
ASP.NET实现二维码 using System;using System.Collections.Generic;using System.Drawing;using System.Linq;us ...
- 实体类转Json的2种方法
首先申明所需jar包: ezmorph-1.0.6.jar jackson-all-1.7.6.jar jsoup-1.5.2.jar 一.创建一个实体类Emp. package com.hyx.en ...
- SpringMVC通过实体类返回json格式的字符串,并在前端显示
一.除了搭建springmvc框架需要的jar包外,还需要这两个jar包 jackson-core-asl-1.9.2.jar和jackson-mapper-asl-1.9.2.jar 二.web,. ...
- C# 实体类转json数据过滤掉字段为null的字段
C# 实体类转json数据过滤掉字段为null的字段 语法如下: var jsonSetting = new JsonSerializerSettings {NullValueHandling = N ...
- json字符串和json对象的相互转化
开发经常要用到json字符串和json对象的相互转化,这里总结常用的两个函数.JSON.parse('字符串'),JSON.stringify('json对象') <script type=&q ...
随机推荐
- 让你头晕的VR头显,背后发生了什么?
随着虚拟现实渐渐兴起,国内现在做虚拟现实的厂商也增多了起来.但是我经常听到有体验者向我表示:他戴上国外大厂诸如Oculus.Sony和Valve的VR头显的时候,体验十分出色,但是戴上国产的VR头显, ...
- [Xcode 实际操作]二、视图与手势-(7)UIView视图的渐变填充
目录:[Swift]Xcode实际操作 本文将演示创建一个具有渐变填充色的图形 import UIKit class ViewController: UIViewController { overri ...
- JavaScript 原生提供两个 Base64 相关的方法
JavaScript 原生提供两个 Base64 相关的方法. btoa():任意值转为 Base64 编码 atob():Base64 编码转为原来的值 var string = 'Hello Wo ...
- Java | 基础归纳 | 静态方法与实例方法的区别
静态方法和实例方法的区别主要体现在两个方面: 在外部调用静态方法时,可以使用"类名.方法名"的方式,也可以使用"对象名.方法名"的方式.而实例方法只有后面这种方 ...
- mongodb javaapi网页版链接
http://www.open-open.com/doc/view/abe58dc8d0114ef2bd34d0bbccd3691e
- [题解](树的计数)luogu_P4430猴子打架_/_luogu_P4981父子
来源:题解 比较不错的博客:http://www.cnblogs.com/dirge/p/5503289.html 最后生成一颗无根树,有n^(n-2)种情况,打架的顺序有(n-1)!种 #inclu ...
- Linux (二)
PS :显示系统进程 -a :显示所有进程(包括其他用户的进程) -u :用户以及其他详细信息 -x :显示没有控制终端的进程 -ef :显示所有 top :用于动态地监视进程活动与系统负载的信息 p ...
- 安装gnvm (windows下nodejs版本管理工具)
一些写在前面的话,为什么装这个?前两天看avalon视频的时候,里面有介绍去哪儿的前端构建工具fekit.我这人吧,好奇心特别强,就打算安装用用看.在安装时它提示要求node版本0.8.x,所以我选择 ...
- jquery.validate自定义验证--成功提示与择要提示
1. 自定义验证--成功提示 1) 添加选项 errorClass: "unchecked", validClass: "checked", errorElem ...
- Linux查看某个端口是否启动
查看命令 netstat -an | 执行结果: [root@test ~]# netstat -an | tcp 0.0.0.0:* LISTEN 有tcp 这一行返回说明已开放