要使程式 可以執行 必須引入JSON-lib包,JSON-lib包同時依賴以下的JAR包:
1.commons-lang.jar
2.commons-beanutils.jar
2.commons-beanutils.jar
4.commons-logging.jar
5.ezmorph.jar
6.json-lib-2.2.2-lib-2.2.2-k15.jar
s,java arrays 和XML和JSON互相轉換的套件。在本例中,我們將使用JSONObject類別建立JSONObject對象,然後我們列印這些對象的值。為了使用 JSONObject對象,我們要引入"net.sf.json"套件。為了為物件添加元素,我們要使用put()方法。
import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class JSONObjectSample { //创建JSONObject对象 private static JSONObject createJSONObject(){ JSONObject jsonObject = new JSONObject(); jsonObject.put("name", "kevin"); jsonObject.put("Max.score", new Integer(100)); jsonObject.put("Min.score", new Integer(50)); jsonObject.put("nickname", "picglet"); return jsonObject; } public static void main(String[] args) { JSONObject jsonObject = JSONObjectSample.createJSONObject(); //输出jsonobject对象 System.out.println("jsonObject==>"+jsonObject); //判读输出对象的类型 boolean isArray = jsonObject.isArray(); boolean isEmpty = jsonObject.isEmpty(); boolean isNullObject = jsonObject.isNullObject(); System.out.println("isArray:"+isArray+" isEmpty:"+isEmpty+" isNullObject:"+isNullObject); //添加属性 jsonObject.element("address", "swap lake"); System.out.println("添加属性后的对象==>"+jsonObject); //返回一个JSONArray对象 JSONArray jsonArray = new JSONArray(); jsonArray.add(0, "this is a jsonArray value"); jsonArray.add(1,"another jsonArray value"); jsonObject.element("jsonArray", jsonArray); JSONArray array = jsonObject.getJSONArray("jsonArray"); System.out.println("返回一个JSONArray对象:"+array); //添加JSONArray后的值 //{"name":"kevin","Max.score":100,"Min.score":50,"nickname":"picglet","address":"swap lake", //"jsonArray":["this is a jsonArray value","another jsonArray value"]} System.out.println(jsonObject); //根据key返回一个字符串 String jsonString = jsonObject.getString("name"); System.out.println("jsonString==>"+jsonString); } } import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class JSONObjectSample { //创建JSONObject对象 private static JSONObject createJSONObject(){ JSONObject jsonObject = new JSONObject(); jsonObject.put("name", "kevin"); jsonObject.put("Max.score", new Integer(100)); jsonObject.put("Min.score", new Integer(50)); jsonObject.put("nickname", "picglet"); return jsonObject; } public static void main(String[] args) { JSONObject jsonObject = JSONObjectSample.createJSONObject(); //输出jsonobject对象 System.out.println("jsonObject==>"+jsonObject); //判读输出对象的类型 boolean isArray = jsonObject.isArray(); boolean isEmpty = jsonObject.isEmpty(); boolean isNullObject = jsonObject.isNullObject(); System.out.println("isArray:"+isArray+" isEmpty:"+isEmpty+" isNullObject:"+isNullObject); //添加属性 jsonObject.element("address", "swap lake"); System.out.println("添加属性后的对象==>"+jsonObject); //返回一个JSONArray对象 JSONArray jsonArray = new JSONArray(); jsonArray.add(0, "this is a jsonArray value"); jsonArray.add(1,"another jsonArray value"); jsonObject.element("jsonArray", jsonArray); JSONArray array = jsonObject.getJSONArray("jsonArray"); System.out.println("返回一个JSONArray对象:"+array); //添加JSONArray后的值 //{"name":"kevin","Max.score":100,"Min.score":50,"nickname":"picglet","address":"swap lake", //"jsonArray":["this is a jsonArray value","another jsonArray value"]} System.out.println(jsonObject); //根据key返回一个字符串 String jsonString = jsonObject.getString("name"); System.out.println("jsonString==>"+jsonString); } }