JsonTools.java :java
package com.lihua.json.tools; import net.sf.json.JSONObject; public class JsonTools { public JsonTools() { } /** * @param key * 表示json字符串的頭信息 * @param value * 是對解析的集合的類型 * @return */ //將數據轉換爲Json public static String createJsonString(String key, Object value) { JSONObject jsonObject = new JSONObject(); jsonObject.put(key, value); return jsonObject.toString(); } }
Person.java:json
package com.lihua.json.domain; public class Person { private int id; private String name; private String address; public Person() { } public Person(int id, String name, String address) { super(); this.id = id; this.name = name; this.address = address; } 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 String getAddress() { return address; } public void setAddress(String address) { this.address = address; } //重寫toString()方法 @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", address=" + address + "]"; } }
JsonService.java :dom
package com.lihua.json.service; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.lihua.json.domain.Person; public class JsonService { public JsonService() { } //聲明一個Person類型的方法,併爲其添加單個對象 public Person getPerson() { Person person = new Person(1001,"jack","beijing"); return person; } //聲明一個List<Person>類型的方法,併爲其添加多個對象 public List<Person> getlistPerson() { List<Person> list = new ArrayList<Person>(); Person person1 = new Person(100,"jack","廣東"); Person person2 = new Person(101,"rose","廣西"); list.add(person1); list.add(person2); return list; } //聲明一個List<String>類型的方法,併爲其添加單個對象 public List<String> getlistString() { List<String> list = new ArrayList<String>(); list.add("北京"); list.add("上海"); list.add("廣東"); list.add("廣西"); return list; } //聲明一個List<Map<String,Object>>類型的方法,併爲其添加多個對象 public List<Map<String,Object>> getListMap() { List<Map<String,Object>> list = new ArrayList<Map<String,Object>>(); Map<String,Object> map1 = new HashMap<String,Object>(); map1.put("id", 100); map1.put("name", "jack"); map1.put("address", "北京"); Map<String,Object> map2 = new HashMap<String,Object>(); map2.put("id", 100); map2.put("name", "rose"); map2.put("address", "上海"); list.add(map1); list.add(map2); return list; } }
Test.java :ide
package com.lihua.json.test; import java.util.List; import java.util.Map; import com.lihua.json.domain.Person; import com.lihua.json.service.JsonService; import com.lihua.json.tools.JsonTools; public class Test { public Test() { } public static void main(String[] args) { String msg = ""; //new一個JsonService對象 JsonService service = new JsonService(); //聲明一個Person對象,並取得person對象中的數據 Person person = service.getPerson(); //將person對象中的數據轉換爲json數據,並保存到msg中 msg = JsonTools.createJsonString("person", person); System.out.println("---->"+msg); List<Person> list = service.getlistPerson(); msg = JsonTools.createJsonString("person", list); System.out.println("---->"+msg); List<Map<String, Object>> listmap = service.getListMap(); msg = JsonTools.createJsonString("person", listmap); System.out.println("---->"+msg); } }
運行結果:this