net.sf.json------json解析
下載地址java
- 本次使用版本:http://sourceforge.net/projects/json-lib/files/json-lib/json-lib-1.1/
- 最新版本:http://sourceforge.net/projects/json-lib/files/json-lib/
使用net.sf.json須要導入的包

JSONObject
- package com.itlwc.test;
-
- import net.sf.json.JSONArray;
- import net.sf.json.JSONObject;
-
- public class Test {
- public static void main(String[] args) {
-
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("username", "lwc");
- jsonObject.put("password", "123");
-
- System.out.println(jsonObject);
-
-
- jsonObject.element("sex", "男");
- System.out.println(jsonObject);
-
-
- System.out.println(jsonObject.get("sex"));
-
-
- boolean isArray = jsonObject.isArray();
- boolean isEmpty = jsonObject.isEmpty();
- boolean isNullObject = jsonObject.isNullObject();
-
- System.out.println("是否數組:" + isArray + " 是否空:" + isEmpty + " 是否空對象:"
- + isNullObject);
-
-
- JSONArray jsonArray = new JSONArray();
- jsonArray.add(0, "lwc");
- jsonArray.add(1, "nxj");
-
- jsonObject.element("student", jsonArray);
-
- System.out.println(jsonObject);
- }
- }
-
-
-
-
-
-
-
-
JSONArray
- package com.itlwc.test;
-
- import net.sf.json.JSONArray;
- import net.sf.json.JSONObject;
-
- public class Test {
- public static void main(String[] args) {
-
- JSONArray jsonArray = new JSONArray();
- jsonArray.add(0, "lwc");
- jsonArray.add(1, "nxj");
- jsonArray.element("mxj");
-
- System.out.println(jsonArray);
-
-
- System.out.println(jsonArray.get(0));
-
-
- jsonArray.set(0, "itlwc");
- System.out.println(jsonArray);
-
-
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("username", "lwc");
- jsonObject.put("password", "123");
-
- jsonArray.add(jsonObject);
- System.out.println(jsonArray);
-
-
- for(int i=0;i<jsonArray.size();i++){
- System.out.print(jsonArray.get(i)+"\t");
- }
- }
- }
-
-
-
-
-
-
-
-
JavaBean與json字符串互轉
- package com.itlwc.test;
-
- import net.sf.json.JSONObject;
-
- import com.itlwc.entity.Student;
-
- public class Test {
- public static void main(String[] args) {
-
- Student stu1 = new Student("lwc", "111111");
- JSONObject jsonObject = JSONObject.fromObject(stu1);
- System.out.println(jsonObject);
-
-
- String jsondata = "{\"password\":\"111111\",\"username\":\"lwc\"}";
- JSONObject jsonObject1 = JSONObject.fromObject(jsondata);
- Student stu2 = (Student) JSONObject.toBean(jsonObject1, Student.class);
- System.out.println(stu2);
- }
- }
-
-
-
-
-
List與json字符串互轉
- package com.itlwc.test;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import net.sf.json.JSONArray;
- import net.sf.json.JSONObject;
-
- import com.itlwc.entity.Student;
-
- public class Test {
- public static void main(String[] args) {
-
- List list = new ArrayList();
- list.add(new Student("lwc", "111111"));
- list.add(new Student("nxj", "222222"));
- JSONArray jsonArray = JSONArray.fromObject(list);
- System.out.println(jsonArray);
-
-
- List list1 = new ArrayList();
- String jsondata = "[{\"password\":\"111111\",\"username\":\"lwc\"},{\"password\":\"222222\",\"username\":\"nxj\"}]";
- JSONArray jsonArray1 = JSONArray.fromObject(jsondata);
- for (int i = 0; i < jsonArray1.size(); i++) {
- JSONObject jsonObject2 = jsonArray1.getJSONObject(i);
- Student stu2 = (Student) JSONObject.toBean(jsonObject2,
- Student.class);
- list1.add(stu2);
- }
- System.out.println(list1);
- }
- }
-
-
-
-
-
Map與json字符串互轉
- package com.itlwc.test;
-
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
-
- import net.sf.json.JSONObject;
-
- import com.itlwc.entity.Student;
-
- public class Test {
- public static void main(String[] args) {
-
- Map map = new HashMap();
- map.put("1", new Student("lwc", "111111"));
- map.put("2", new Student("nxj", "222222"));
- JSONObject jsonMap = JSONObject.fromObject(map);
- System.out.println(jsonMap);
-
-
- String jsondata = "{\"2\":{\"password\":\"222222\",\"username\":\"nxj\"},\"1\":{\"password\":\"111111\",\"username\":\"lwc\"}}";
- Map map1 = (Map) JSONObject.fromObject(jsondata);
- Set set = map1.keySet();
- Iterator ite = set.iterator();
- while (ite.hasNext()) {
- String key = (String) ite.next();
- JSONObject jsonObject = JSONObject.fromObject(map1.get(key));
- Student stu = (Student) JSONObject
- .toBean(jsonObject, Student.class);
- System.out.println(key + " " + stu);
- }
-
- }
- }
-
-
-
-
-
-
JSONArray與List互轉
- package com.itlwc.test;
-
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
-
- import net.sf.json.JSONArray;
- import net.sf.json.JsonConfig;
-
- import com.itlwc.entity.Student;
-
- public class Test {
- public static void main(String[] args) {
-
- List<Student> list = new ArrayList<Student>();
- list.add(new Student("lwc", "111111"));
- list.add(new Student("nxj", "222222"));
- JSONArray jsonArray = JSONArray.fromObject(list);
- System.out.println(jsonArray.toString());
-
-
- List<Student> list2 = JSONArray.toList(jsonArray, new Student(), new JsonConfig());
- Iterator<Student> ite = list2.iterator();
- while(ite.hasNext()){
- Student stu =ite.next();
- System.out.println(stu);
- }
- }
- }
-
-
-
-
-
-
JSONArray與數組互轉
- package com.itlwc.test;
-
- import net.sf.json.JSONArray;
-
- public class Test {
- public static void main(String[] args) {
-
- boolean[] boolArray = new boolean[] { true, false, true };
- JSONArray jsonArray = JSONArray.fromObject(boolArray);
- System.out.println(jsonArray.toString());
-
-
- Object obj[] = jsonArray.toArray();
- for (Object o : obj) {
- System.out.print(o + " ");
- }
- }
- }
-
-
-
-
-
XML與JSON互轉
須要導入xom-1.1.jarjson
- package com.itlwc.test;
-
- import net.sf.json.JSON;
- import net.sf.json.JSONObject;
- import net.sf.json.xml.XMLSerializer;
-
- public class Test {
- public static void main(String[] args) throws Exception {
-
- String xml = "<root>" + "<name type='type'>zhaipuhong</name>"
- + "<gender>male</gender>" + "<birthday>" + "<year>1970</year>"
- + "<month>12</month>" + "<day>17</day>" + "</birthday>"
- + "</root>";
- XMLSerializer xmlSerializer = new XMLSerializer();
- JSON json = xmlSerializer.read(xml);
- System.out.println(json.toString(2));
-
-
- String jsondata = "{\"root\":{" + "\"name\":\"zhaipuhong\","
- + "\"gender\":\"male\"," + "\"birthday\":{"
- + "\"year\":\"1970\"," + "\"month\":\"12\"," + "\"day\":\"17\""
- + "}" + "}" + "}";
- JSONObject jsonObject = JSONObject.fromObject(jsondata);
- String xmlstr = new XMLSerializer().write(jsonObject);
- System.out.println(xmlstr);
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
歡迎關注本站公眾號,獲取更多信息