1、创建类Json
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
//将Java数据转换成Json数据
public class Json {
public static void main(String[] args) {
Json json = new Json();
json.array2json(); //从数组转换到JSON
System.out.println("----------------------------------");
json.string2Json();//从字符串转换到JSON
System.out.println("----------------------------------");
json.list2json(); //从列表转换到JSON
System.out.println("----------------------------------");
json.createJson(); //从一般数据转换到JSON
System.out.println("----------------------------------");
json.map2json(); //从map转换到JSON
System.out.println("----------------------------------");
json.bean2json(); //从Bean转换到JSON
System.out.println("----------------------------------");
json.json2bean(); //从JSON转换到Bean
System.out.println("----------------------------------");
}
// 数组转换成Json
public void array2json() {
String[] numbers = { "one", "two", "three", "four", "five" };
JSONArray ja = JSONArray.fromObject(numbers);
System.out.println(ja);
}
//字符串转换成Json
public void string2Json() {
String json = "{name:\"jack\",sex:\"male\",age:23}";
JSONObject jo = JSONObject.fromObject(json);
System.out.println(jo);
}
// 从集合转换成Json
public void list2json() {
List<String> list = new ArrayList<String>();
list.add("first");
list.add("second");
list.add("third");
list.add("fourth");
JSONArray ja = JSONArray.fromObject(list);
System.out.println(ja);
}
// 一般数据转换成Json
public void createJson() {
JSONArray ja = JSONArray.fromObject("['json','is','easy']");
System.out.println(ja);
}
// map转换成Json
public void map2json() {
Map<String,Object> map = new HashMap<String, Object>();
map.put("name", "Howard");
map.put("bool", Boolean.TRUE);
map.put("integer", new Integer(1));
map.put("array", new String[] { "a", "b", "c", "d" });
map.put("function", "function(i){ return this.arr[i]; }");
JSONObject jo = JSONObject.fromObject(map);
System.out.println(jo);
}
// Bean转换成Json
public void bean2json() {
// fromObject --从其它对象转化成JSON对象
Student student=new Student("Alice",21);
JSONObject jo = JSONObject.fromObject(student);
System.out.println(jo);
}
//Json转换成Bean
public void json2bean() {
String strJson = "{name:\"Jack\",age:23}";
JSONObject jo = JSONObject.fromObject(strJson);
Student student = (Student) JSONObject.toBean(jo, Student.class);
System.out.println(student.getName()+", "+student.getAge());
}
}
2、创建类Student
public class Student {
private String name;
private int age;
public Student(){
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
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;
}
}
3、运行结果
["one","two","three","four","five"]
----------------------------------
{"name":"jack","sex":"male","age":23}
----------------------------------
["first","second","third","fourth"]
----------------------------------
["json","is","easy"]
----------------------------------
{"integer":1,"name":"Howard","bool":true,"function":function(i){ return this.arr[i]; },"array":["a","b","c","d"]}
----------------------------------
{"age":21,"name":"Alice"}
----------------------------------
Jack, 23
----------------------------------