一、jsp中 json对象和json的string类型形式的转换
1、json---->String
var jsonObj = {
"id":11,
"name":"mmm"
};
//将json对象转换为字符串
var str = JSON.stringify(jsonObj);
2、String---->json
//将字符串转换为json对象
var json = JSON.parse(str);
二、java中的java对象与json的字符串形式之间的转换(使用Gson jar包)
2.1、java对象---->json的字符串形式
Person per = new Person(1,"aa",200.0,22);
String jsonStr = new Gson().toJson(per);
//输出结果:{"id":1,"name":"aa","salary":200.0,"age":22}
2.2 、java对象集合--->json对象数组的字符串形式
List<Person> personList = new ArrayList<Person>();
personList.add(new Person(2, "bb", 300, 24));
personList.add(new Person(3, "cc", 400, 25));
personList.add(new Person(4, "dd", 500, 26));
String json1 = new Gson().toJson(personList);
System.out.println(json1);
//输出结果为:[{"id":2,"name":"bb","salary":300.0,"age":24}, {"id":3,"name":"cc","salary":400.0,"age":25},{"id":4,"name":"dd","salary":500.0,"age":26}]
3.1、json的字符串形式--->java对象
String str = "{"id":1,"name":"aa","salary":200.0,"age":22}";
Person fromJson = new Gson().fromJson(str, Person.class);
System.out.println(fromJson);
//输出结果为:Person [id=1, name=aa, salary=200.0, age=22]
3.2、json对象数组的字符串形式--->java对象集合
String str2 = "[{"id":2,"name":"bb","salary":300.0,"age":24},{"id":3,"name":"cc","salary":400.0,"age":25},{"id":4,"name":"dd","salary":500.0,"age":26}]";
List<Person> personsList1 = new Gson().fromJson(str2,new PersonTypeToken().getType());//新建PersonTypeToken类继承TypeToken类
System.out.println(personsList1);
输出结果为:[Person [id=2, name=bb, salary=300.0, age=24], Person [id=3, name=cc, salary=400.0, age=25], Person [id=4, name=dd, salary=500.0, age=26]]
1、json---->String
var jsonObj = {
"id":11,
"name":"mmm"
};
//将json对象转换为字符串
var str = JSON.stringify(jsonObj);
2、String---->json
//将字符串转换为json对象
var json = JSON.parse(str);
二、java中的java对象与json的字符串形式之间的转换(使用Gson jar包)
2.1、java对象---->json的字符串形式
Person per = new Person(1,"aa",200.0,22);
String jsonStr = new Gson().toJson(per);
//输出结果:{"id":1,"name":"aa","salary":200.0,"age":22}
2.2 、java对象集合--->json对象数组的字符串形式
List<Person> personList = new ArrayList<Person>();
personList.add(new Person(2, "bb", 300, 24));
personList.add(new Person(3, "cc", 400, 25));
personList.add(new Person(4, "dd", 500, 26));
String json1 = new Gson().toJson(personList);
System.out.println(json1);
//输出结果为:[{"id":2,"name":"bb","salary":300.0,"age":24}, {"id":3,"name":"cc","salary":400.0,"age":25},{"id":4,"name":"dd","salary":500.0,"age":26}]
3.1、json的字符串形式--->java对象
String str = "{"id":1,"name":"aa","salary":200.0,"age":22}";
Person fromJson = new Gson().fromJson(str, Person.class);
System.out.println(fromJson);
//输出结果为:Person [id=1, name=aa, salary=200.0, age=22]
3.2、json对象数组的字符串形式--->java对象集合
String str2 = "[{"id":2,"name":"bb","salary":300.0,"age":24},{"id":3,"name":"cc","salary":400.0,"age":25},{"id":4,"name":"dd","salary":500.0,"age":26}]";
List<Person> personsList1 = new Gson().fromJson(str2,new PersonTypeToken().getType());//新建PersonTypeToken类继承TypeToken类
System.out.println(personsList1);
输出结果为:[Person [id=2, name=bb, salary=300.0, age=24], Person [id=3, name=cc, salary=400.0, age=25], Person [id=4, name=dd, salary=500.0, age=26]]