一、JSONObject和JSONArray的數(shù)據(jù)表示形式
JSONObject的數(shù)據(jù)是用 { } 來(lái)表示的,
例如: { "id" : "123", "courseID" : "huangt-test", "title" : "提交作業(yè)", "content" : null }
而JSONArray,顧名思義是由JSONObject構(gòu)成的數(shù)組,用 [ { } , { } , ...... , { } ] 來(lái)表示
例如: [ { "id" : "123", "courseID" : "huangt-test", "title" : "提交作業(yè)" } , { "content" : null, "beginTime" : 1398873600000 "endTime" } ] ;
表示了包含2個(gè)JSONObject的JSONArray。
可以看到一個(gè)很明顯的區(qū)別,一個(gè)最外面用的是 { } ,一個(gè)最外面用的是 [ ] ;
二、如何從字符串String獲得JSONObject對(duì)象和JSONArray對(duì)象
JSONObject jsonObject = new JSONObject ( String str); JSONArray jsonArray = new JSONArray(String str ) ;
三、如何從JSONArray中獲得JSONObject對(duì)象
大家可以把JSONArray當(dāng)成一般的數(shù)組來(lái)對(duì)待,只是獲取的數(shù)據(jù)內(nèi)數(shù)據(jù)的方法不一樣
JSONObject jsonObject = jsonArray.getJSONObject(i) ;
四、獲取JSON內(nèi)的數(shù)據(jù)
int mid= jsonObject.getInt ( "id" ) ; // 這里的mid得到的數(shù)據(jù)就是123. String mcourse=jsonObject.getString( " courseID") ; // 這里的mcourse得到的數(shù)據(jù)就是huangt-test.
總結(jié)
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注