一、JSONObject和JSONArray的數據表示形式
JSONObject的數據是用 { } 來表示的,
例如: { "id" : "123", "courseID" : "huangt-test", "title" : "提交作業", "content" : null }
而JSONArray,顧名思義是由JSONObject構成的數組,用 [ { } , { } , ...... , { } ] 來表示
例如: [ { "id" : "123", "courseID" : "huangt-test", "title" : "提交作業" } , { "content" : null, "beginTime" : 1398873600000 "endTime" } ] ;
表示了包含2個JSONObject的JSONArray。
可以看到一個很明顯的區別,一個用的是 { } ,一個最外面用的是 [ ] ;
二、如何從字符串String獲得JSONObject對象和JSONArray對象
JSONObject jsonObject = new JSONObject ( String str);
JSONArray jsonArray = new JSONArray(String str ) ;
三、如何從JSONArray中獲得JSONObject對象
大家可以把JSONArray當成一般的數組來對待,只是獲取的數據內數據的方法不一樣
JSONObject jsonObject = (JSONObject)jsonArray.get(i);
JSONObject jsonObject = jsonArray.getJSONObject(i) ;
兩者都可。第一種注意轉換