Java对JSON的简单操作
JSONArray和JSONObject
基本用法
package com.cloud.test;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class Demo1 {
//创建JSONObject对象
public static JSONObject createJSONObject(){
JSONObject jsonObject = new JSONObject();
jsonObject.put("username", "Spring");
jsonObject.put("sex", "男");
jsonObject.put("QQ", "123456");
return jsonObject;
}
@SuppressWarnings("static-access")
public static void main(String[] args) throws Exception {
//创建一个JSONObject对象
JSONObject jsonObject = Demo1.createJSONObject();
//输出jsonObject:{"sex":"男","username":"Spring","QQ":"123456"}
System.out.println(jsonObject);
//判断jsonObject的对象类
System.out.println("数组?"+jsonObject.isArray()+";为空?"+
jsonObject.isEmpty()+";isNullObject?"+
jsonObject.isNullObject()+";");
//追加元素属性
jsonObject.put("address", "安徽合肥");
System.out.println(jsonObject);
// 返回一个JSONArray对象
JSONArray jsonArray = new JSONArray();
jsonArray.add(0, "Value0");
jsonArray.add(1, "Value1");
jsonObject.element("jsonArray", jsonArray);
//在jsonObject后面住家一个jsonArray
JSONArray array = jsonObject.getJSONArray("jsonArray");
System.out.println(jsonObject);
System.out.println("返回一个JSONArray对象:" + array);
//根据key返回字符串
String username = jsonObject.getString("username");
System.out.println("username:"+username);
//字符串转换为jsonObject
String temp = jsonObject.toString();
System.out.println(temp);
JSONObject object = jsonObject.fromObject(temp);
System.out.println("QQ:"+object.get("QQ"));
}
}案例
package com.cloud.test;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class Demo2 {
public static void main(String[] args) {
JSONObject jsonObject0 = new JSONObject();
JSONObject jsonObject1 = new JSONObject();
JSONObject jsonObject2 = new JSONObject();
JSONObject jsonObject3 = new JSONObject();
JSONArray jsonArray = new JSONArray();
//jsonObject0
jsonObject0.put("name0", "Spring");
jsonObject0.put("sex0", "man");
System.out.println("jsonObject0:"+jsonObject0);
//jsonObject1
jsonObject1.put("name1", "summer");
jsonObject1.put("sex1", "woman");
System.out.println("jsonObject1"+jsonObject1);
//jsonObject2
jsonObject2.put("item0", jsonObject0);
jsonObject2.put("item1", jsonObject1);
System.out.println("jsonObject2"+jsonObject2);
//jsonObject3
jsonObject3.put("jo31", jsonObject2);
jsonObject3.put("jo32", jsonObject3);
System.out.println("jsonObject3"+jsonObject3);
//往JSONArray中添加JSONObject对象。发现JSONArray跟JSONObject的区别就是JSONArray比JSONObject多中括号[]
jsonArray.add(jsonObject1);
System.out.println(jsonArray);
JSONObject jsonObject4 = new JSONObject();
jsonObject4.element("weater", jsonArray);
System.out.println("jsonObject4:"+jsonObject4);
}
}FromObject和toBean
fromObject
package com.cloud.test;
import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONObject;
public class fo {
@SuppressWarnings("rawtypes")
public static void main(String[] args) {
Map map = new HashMap();
map.put("spring", "spring1");
map.put("summer", "summer1");
map.put("autumn", "autumn1");
JSONObject json = JSONObject.fromObject(map);
//{"autumn":"autumn1","summer":"summer1","spring":"spring1"}
System.out.println(json);
}
}toBean
student.java实体类
package com.cloud.test;
public class Student{
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
public String toString(){
return this.id+";"+this.name+";"+this.age;
}
}tb.java
package com.cloud.test;
import net.sf.json.JSONObject;
public class tb {
public static void main(String[] args) {
String json = "{id:'101',name:'你好',age:'22'}";
Student stu = new Student();
JSONObject jsonObject = JSONObject.fromObject(json);
stu = (Student) JSONObject.toBean(jsonObject, Student.class);
//101;你好;22
System.out.println(stu);
String json1 = "{id:'101',name:'张三'}";
Student stu1 = new Student();
JSONObject obj = JSONObject.fromObject(json1);
System.out.println("obj:"+obj);
stu1 = (Student)JSONObject.toBean(obj, Student.class);
//101;张三;0
System.out.println(stu1);
String json2 = "{id:'101',age:'22'}";
Student stu2 = new Student();
JSONObject obj1 = JSONObject.fromObject(json2);
stu2 = (Student)JSONObject.toBean(obj1, Student.class);
//101;null;22
System.out.println(stu2);
String json3 = "{id:'101',name:'张三',age:'nn'}";
Student stu3 = new Student();
JSONObject obj2 = JSONObject.fromObject(json3);
stu3 = (Student)JSONObject.toBean(obj2, Student.class);
//101;张三;0
System.out.println(stu3);
String json4 = "{id:'101',name:'张三',age:'22',sex:'男'}";
Student stu4 = new Student();
JSONObject obj3 = JSONObject.fromObject(json4);
stu4 = (Student)JSONObject.toBean(obj3, Student.class);
//101;张三;22
System.out.println(stu4);
}
}