天天看點

将Java資料轉換成Json資料

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 ----------------------------------