JSON
来源:互联网 发布:数据库insert语句用法 编辑:程序博客网 时间:2024/06/08 19:28
一、JSON是一种特殊的字符
二、对象和Json之间的转换
<1> 对象--->字符串:
JsonObject jsonObject = JsonObject.fromObject ( 字符串 );
@Test
public void testObjectToJson(){
Person person=new Person(1, "亮亮", 17);
JSONObject jsonObject=JSONObject.fromObject(person);
System.out.println(jsonObject.toString());
}
<2> 字符串--->对象:
1、JsonObject jsonObject = JsonObject.fromObject ( 字符串 );
2、对象=jsonObject .toBean ( jsonObject , 对象.class );
@Test
public void testJsonToObject(){
Person person=new Person(1, "亮亮", 17);
JSONObject jsonObject=JSONObject.fromObject(person);
String s=jsonObject.toString();
Person person2=(Person) JSONObject.toBean(jsonObject, Person.class);
System.out.println(person2);
}
三、对象集合和字符串之间的转换
<1>对象集合--->字符串
JsonArray jsonArray=JsonArray.fromObject(对象集合);
<2>字符串---->对象集合
JsonArray jsonArray=JsonArray.fromObject(字符串);
对象集合=jsonArray.toCollection(jsonArray);
/**
* 对象集合转成Json(常用)
*/
@Test
public void testArrayToJson(){
List<Person> persons=new ArrayList<Person>();
for (int i = 1; i <=36; i++) {
persons.add(new Person(i, "G160628S"+i, 18));
}
JSONArray jsonArray=JSONArray.fromObject(persons);
System.out.println(jsonArray.toString());
}
/**
* 将Json转成对象集合
*/
@Test
public void testJsonToArray(){
List<Person> persons=new ArrayList<Person>();
for (int i = 1; i <=36; i++) {
persons.add(new Person(i, "G160628S"+i, 18));
}
JSONArray jsonArray=JSONArray.fromObject(persons);
List<Person> persons2=(List<Person>) JSONArray.toCollection(jsonArray, Person.class);
for (Person person : persons2) {
System.out.println(person);
}
}
四、在JSP中使用Json
在jsp页面引入json.js文件
<1>Json.stringify(对象)----->将JS对象转换成Json字符串
<2>Json.parse-------->将Json字符串转成Js对象
- Json
- JSON
- JSON
- JSON
- json
- JSON
- JSON
- json
- Json
- json
- JSON
- json
- json
- JSON
- json
- JSON
- JSON
- json
- spring security for rest web service
- 欢迎使用CSDN-markdown编辑器
- 宽容才能发现美好
- stract2中action中的type中redirectActio,redirectAction,chain的详解
- 第十三周
- JSON
- SQL Server 克隆数据库到同一服务器
- A Simple Math Problem(矩阵快速幂(模板))
- k近邻算法的kd树实现原理
- JSP中9大内置对象详解
- HTML
- oracle 小编程
- Gboard背后的机器学习
- 高矮脉冲数据提取分离,以及连续计数程序结构