最近在學習過程當中用到了稍微複雜點的json數據須要將json數據解析出來,這裏就截取一部分做爲例子java
JSONObject-lib包是一個beans,collections,maps,java arrays和xml和JSON互相轉換的包。json
http://files.cnblogs.com/java-pan/lib.rar數組
*或者在Maven的pom.xml文件中直接配置以下:學習
<dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency>
json數據:spa
{ "cartypes":[ {"id":1,"imgUrl":"img/l.jpg","bigimg": "img/d.jpg","title":"別克威朗","marketprice":"15.29","periods":"12", "endrepayone":"96800","endrepaytwo":"96800","endrepaythree":"93000", "endmonthone":"3408","endmonthtwo":"3408","endmonththree":"3278", "repayfirst":"15290","repaytwo":"22935", "repaythree":"30580", "monthrepayone":"3578","monthrepaytwo":"2878","monthrepaythree":"2478", "cardetails":
[{ "imageId00": "img/first-bkwl.jpg", "imageId01": "img/bkwl01.jpg", "imageId02": "img/bkwl02.jpg", "imageId03": "img/bkwl03.jpg", "imageId04": "img/bkwl04.jpg", "carname": "別克", "carmatter": "威朗", "carvolume":"1.5L", "sitnum":"5", "cargearbox":"6擋手自一體", "caremission":"國V", "carldone":"一體式座艙", "carldtwo":"絨面內飾", "carldthree":"全景天窗", "carldfour":"展翼型HID大燈" }] }, {"id":2,"imgUrl":"img/l.jpg","bigimg": "img/d.jpg","title":"英菲尼迪","marketprice":"18.98","periods":"12", "endrepayone":"126800","endrepaytwo":"126800","endrepaythree":"126800", "endmonthone":"4458","endmonthtwo":"4458","endmonththree":"4458", "repayfirst":"18980","repaytwo":"28470", "repaythree":"37960", "monthrepayone":"2738","monthrepaytwo":"1878","monthrepaythree":"998", "cardetails":
[{
"imageId00": "img/first.jpg", "imageId01": "img/yfnd01.jpg", "imageId02": "img/yfnd02.jpg", "imageId03": "img/yfnd03.jpg", "imageId04": "img/yfnd04.jpg", "carname": "英菲尼迪", "carmatter": "ESQ", "carvolume":"1.6L", "sitnum":"5", "cargearbox":"CVT無級變速", "caremission":"國V", "carldone":"定製輪轂", "carldtwo":"多功能方向盤", "carldthree":"LED尾燈", "carldfour":"真皮座椅" }]
}
] }
當接受到的是上面的json數據時,要獲取到裏面的鍵對應的值應該怎樣作呢,好比要獲取title的值,獲取cardetails中的imageId02的值等。code
面對這樣數組與對象相互嵌套的狀況須要一步步將數據拆分,主要思想仍是根據鍵取值,對於數組類型仍是須要先根據」下標」取出元素。這裏還須要用到JSONObject與JSONArray。xml
將上面的json數據簡化就是:(這裏保留個id便於識別)對象
{ "cartypes":[ { "id":1,"bigimg": "img/dt-bkwl.jpg", "cardetails": [{ "imageId02": "img/bkwl02.jpg}] }
{
"id":2,"bigimg": "img/xxx.jpg",
"cardetails":[{"imageId002":"img/xx.jpg"}]
} ] }
這就是簡化了的json數據,能夠看出這個串最外層是一個大的鍵爲cartypes的對象,而它的值是json數組形式的比較複雜的json數據。繼續分析 [ ]的部分,能夠看到,裏面有兩個數組元素,每一個元素分別是被{ }包起來的json對象,他們的元素組成相同,再看每一個元素裏面包含幾個鍵值對的數據,其中鍵cardetails的值又是一個嵌套的json數組,裏面包含一個json對象。分析完畢。那該怎樣才能(拿到數據)解析呢?blog
通常取數據有兩種方式,看須要選擇。three
方式①:
經過 JSONObject.getString("鍵")直接獲取,這種方式只能每次獲取一個。
方式②
經過構建與json對象相應的bean來獲取。
我在寫上面的例子時用到了兩種方式,因爲須要使用到 id,bigimg以及cardetails中的大部分數據,所以我在使用時將cardetails封裝成一個bean,方便使用,而其餘用到的比較少,所以就直接根據鍵獲取值。
另外須要注意的是,JSONObject,JSONArray分別對應的是json數據的兩種格式。即{"張三" : "男"} , [{ 張三" : " 男" }] ,使用時須要將其轉換成對應的對象。
如(示例):
JSONObject jsonObject = JSONObject.fromObject(json); //將json字符串轉換爲JSONObject JSONArray jsonArray = JSONArray.fromObject(json); //將json字符串轉換爲JSONArray
還有一點須要指出:在取鍵值是始終須要根據鍵取值,從外到內,取內層的鍵的值須要先獲取外層鍵的值,若是跨越取值會報錯。
下面演示取值:
JSONObject jsonObject = JSONObject.fromObject(json); //將json字符串轉化爲JSONObject String cartypes=jsonObject.getString("cartypes"); //經過getString("cartypes")取出裏面的信息 JSONArray jsonArray = JSONArray.fromObject(cartypes); //將取到的cartypes對應的(一組)值字符串轉換爲JSONArray
String id= job.getString("id"); //取id String bigImg = job.getString("bigimg"); //大圖
System.out.println("bigImg:"+bigImg); //能夠顯示已經拿到bigimg的值
因爲cardetails下的基本都是須要的值,一個一個取值比較麻煩,所以將cardetails封裝成一個bean 以下:
Cardetails.java
public class Cardetails { private String imageId00; private String imageId01; private String imageId02; private String imageId03; private String imageId04; private String carname; private String carmatter; private String carvolume; private int sitnum; private String cargearbox; private String caremission; private String carldone; private String carldtwo; private String carldthree; private String carldfour; //get set 方法以及toString方法略 }
到這裏,須要將cardetails中的鍵全轉成Cardetails中的屬性,方法以下:
//將cardetail封裝成bean
JSONArray carDetailArr=job.getJSONArray("cardetails");//將json字符串轉化爲JSONArray JSONObject carDetailObj = carDetailArr.getJSONObject(0);//獲取數組第一個元素 Cardetails cardetails = (Cardetails) JSONObject.toBean(carDetailObj, Cardetails.class);//封裝成bean
System.out.println("cardetails:"+cardetails); //能獲取到數據
最後附上部分代碼:
public void getICarDetail(int id){ String json=null; try { json=iCarDetail.getICarDetail(id);//這裏既是獲取上面json數據 } catch (Exception e) { e.printStackTrace(); } int jsonId=0;//json數組裏的id值 JSONObject jsonObject = JSONObject.fromObject(json); //將json字符串轉化爲JSONObject String cartypes=jsonObject.getString("cartypes");//經過getString("cartypes")取出裏面的信息 JSONArray jsonArray = JSONArray.fromObject(cartypes); //將取到的cartypes對應的(一組)值字符串轉換爲JSONArray //遍歷jsonarray 數組 if(jsonArray.size()>0){ for(int i=0;i<jsonArray.size();i++){ JSONObject job = jsonArray.getJSONObject(i);//把每個對象轉成json對象 jsonId=(int)job.get("id"); //獲得每一個對象中的id值 if(jsonId==id){ //獲取相關值 String title = job.getString("title"); String bigImg = job.getString("bigimg"); String repayFirst = job.getString("repayfirst"); String endrepayone = job.getString("endrepayone"); String endmonthone = job.getString("endmonthone"); String marketprice = job.getString("marketprice");
//將cardetail封裝成bean JSONArray carDetailArr=job.getJSONArray("cardetails");//將json字符串轉化爲JSONArray JSONObject carDetailObj = carDetailArr.getJSONObject(0);//獲取數組第一個元素 Cardetails cardetails = (Cardetails) JSONObject.toBean(carDetailObj, Cardetails.class);//封裝成bean //輸出顯示 System.out.println("******************"); System.out.println("jsonId:"+jsonId); System.out.println("title:"+title); System.out.println("bigImg:"+bigImg); System.out.println("repayFirst:"+repayFirst); System.out.println("endrepayone:"+endrepayone); System.out.println("endmonthone:"+endmonthone); System.out.println("marketprice:"+marketprice); System.out.println("cardetails:"+cardetails);}