JSON-lib包(最關鍵的兩個類分別是JSONObject和JSONArray)完成對json的構造和一些基本方法的使用。 二者區別: ①JSONObject構造的字元串是鍵值對形式(key:value),多個鍵值對間以英文逗號連接; ②JSONArray構造的字元串是數組形式([array1,a ...
JSON-lib包(最關鍵的兩個類分別是JSONObject和JSONArray)完成對json的構造和一些基本方法的使用。
二者區別:
①JSONObject構造的字元串是鍵值對形式(key:value),多個鍵值對間以英文逗號連接;
②JSONArray構造的字元串是數組形式([array1,array2,...])。
需要使用的包下載鏈接:http://pan.baidu.com/s/1o7MZ8X8
一、JSONObject的使用。
(1)JSON字元串的兩種構造方法:
①使用Java對象;②使用Map集合。
步驟一:首先新建Java工程,導入依賴包;
步驟二:建立兩個測試類:
Teacher.java
package com.snnu.json; import java.util.List; public class Teacher { private String name; private String sex; private int age; private List<Transport> myTool; public Teacher(){ } public Teacher(String name,String sex,int age,List<Transport> myTool){ this.name = name; this.sex = sex; this.age = age; this.myTool = myTool; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public List<Transport> getMyTool() { return myTool; } public void setMyTool(List<Transport> myTool) { this.myTool = myTool; } }
Transport.java
package com.snnu.json; public class Transport { private String name; private float price; public Transport(){ } public Transport(String name,float price){ this.name = name; this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } }
步驟三:寫main方法
方式一:
package com.snnu.json; import java.util.ArrayList; import java.util.List; import net.sf.json.JSONObject; public class Demo_creajsonFromObject { // 利用java對象生成json字元串 public JSONObject createJsonFromObject(Object object) { return JSONObject.fromObject(object); } public static void main(String[] args) { // TODO Auto-generated method stub Demo_creajsonFromObject demo = new Demo_creajsonFromObject(); Teacher t = new Teacher(); t.setName("張三"); t.setSex("男"); t.setAge(21); Transport bike = new Transport("自行車", 267); Transport motorcycle = new Transport("摩托車", 3267); Transport car = new Transport("小汽車", 100000); List<Transport> tools = new ArrayList<Transport>(); tools.add(bike); tools.add(motorcycle); tools.add(car); t.setMyTool(tools); JSONObject ob = demo.createJsonFromObject(t); System.out.println(ob); } }
生成的json字元串為:
{
"age": 21,
"myTool": [
{
"name": "自行車",
"price": 267
},
{
"name": "摩托車",
"price": 3267
},
{
"name": "小汽車",
"price": 100000
}
],
"name": "張三",
"sex": "男"
}
方式二:
package com.snnu.json; import java.util.HashMap; import java.util.Map; import net.sf.json.JSONObject; public class Demo_creajsonFromMap { //使用map集合生成json字元串 public JSONObject createJsonFromMap(Map<String,String> map){ JSONObject jsob=new JSONObject(); jsob.putAll(map); return jsob; } public static void main(String[] args) { // TODO Auto-generated method stub Demo_creajsonFromMap demo=new Demo_creajsonFromMap(); Map<String,String> mmap=new HashMap<String,String>(); mmap.put("name", "張三"); mmap.put("sex", "男"); mmap.put("age", "21"); JSONObject ob=demo.createJsonFromMap(mmap); System.out.println(ob); } }
生成的json字元串為:
{ "sex": "男", "name": "張三", "age": "21" }
(2)JSONObject的三個常用方法舉例。
package com.snnu.json; import java.util.ArrayList; import java.util.List; import net.sf.json.JSONObject; public class MethodTest { //put方法:在一個json中插入一個節點,若該節點已存在,則該節點的值將會被替換 public JSONObject testPut(){ JSONObject jo1=new JSONObject(); jo1.put("a", "1"); jo1.put("b", "2"); jo1.put("c", "3"); Transport bike=new Transport("bike",200); jo1.put("d", bike); List<String> list=new ArrayList<String>(); list.add("one"); list.add("two"); list.add("three"); jo1.put("e", list); jo1.put("a", "100"); return jo1; } //accumulate方法:可以在同一個key下累積值,若key對應的value有值,則以數組形式累積;否則相當於put方法 public JSONObject testAccumulate(){ JSONObject jo2=new JSONObject(); jo2.put("a", "1"); jo2.put("b", "2"); jo2.put("c", "3"); jo2.accumulate("c", "300"); Transport bike=new Transport("bike",200); jo2.accumulate("c", bike); List<String> list=new ArrayList<String>(); list.add("one"); list.add("two"); list.add("three"); jo2.accumulate("c", list); jo2.put("d", "4"); return jo2; } //與put方法基本一致 public JSONObject testElement(){ JSONObject jo3=new JSONObject(); jo3.put("a", "1"); jo3.put("b", "2"); jo3.put("c", "3"); jo3.element("c", "300"); return jo3; } public static void main(String[] args) { // TODO Auto-generated method stub MethodTest test=new MethodTest(); System.out.println("JSONObject的put方法使用"+test.testPut()); System.out.println("JSONObject的accumulate方法使用"+test.testAccumulate()); System.out.println("JSONObject的element方法使用"+test.testElement()); } }
①put方法輸出json字元串格式化結果為:
{ "a": "100", "b": "2", "c": "3", "d": { "name": "bike", "price": 200 }, "e": [ "one", "two", "three" ] }
②accumulate方法輸出json字元串格式化結果為:
{ "a": "1", "b": "2", "c": [ "3", "300", { "name": "bike", "price": 200 }, [ "one", "two", "three" ] ], "d": "4" }
③element方法輸出json字元串格式化結果為:
{ "a": "1", "b": "2", "c": "300" }
二、JSONArray的使用
(1)基本使用:
package com.snnu.json; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class demo_JsonArray { public JSONObject testJsonArray(){ JSONObject ob=new JSONObject(); JSONArray ja=new JSONArray(); ja.add("1"); ja.add("2"); ja.add("3"); ja.add("4"); ja.add("5"); ob.put("array", ja); return ob; } public static void main(String[] args) { // TODO Auto-generated method stub demo_JsonArray djs=new demo_JsonArray(); System.out.println("JSONArray的使用:"+djs.testJsonArray()); } }
對輸出後的字元串進行格式化:
{ "array": [ "1", "2", "3", "4", "5" ] }
三、綜合實例
package com.snnu.json; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class demo_testJson { public JSONObject test(){ JSONObject jo=new JSONObject(); jo.put("name", "張三"); jo.put("sex","f"); jo.put("age",21); Transport bike=new Transport("bike",250); jo.put("extra", bike); Transport car=new Transport("car",10000); jo.accumulate("extra", car); Transport motor=new Transport("motor",3000); jo.accumulate("extra", motor); System.out.println(jo); //根據key值(為extra)取對應的value String value=jo.getString("extra"); System.out.println(value); //將字元串轉化為JSONArray JSONArray jsar=JSONArray.fromObject(value); String str_2=String.valueOf(jsar.get(1)); System.out.println(str_2); //將字元串轉化為JSONObject JSONObject jsob=JSONObject.fromObject(str_2); System.out.println("名稱:"+jsob.getString("name")); System.out.println("價錢:"+jsob.getString("price")); System.out.println("-------------------------------分界線-------------------------------------------"); return jo; } public static void main(String[] args) { // TODO Auto-generated method stub demo_testJson dtj=new demo_testJson(); System.out.println("綜合測試:"+dtj.test()); } }
輸出結果為:
{"name":"張三","sex":"f","age":21,"extra":[{"name":"bike","price":250},{"name":"car","price":10000},{"name":"motor","price":3000}]} [{"name":"bike","price":250},{"name":"car","price":10000},{"name":"motor","price":3000}] {"name":"car","price":10000} 名稱:car 價錢:10000 -------------------------------分界線------------------------------------------- 綜合測試:{"name":"張三","sex":"f","age":21,"extra":[{"name":"bike","price":250},{"name":"car","price":10000},{"name":"motor","price":3000}]}