欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解JSONObject和JSONArray區(qū)別及基本用法

 更新時(shí)間:2017年10月25日 15:56:03   作者:kris_cc  
這篇文章主要介紹了詳解JSONObject和JSONArray區(qū)別及基本用法,需要的朋友可以參考下

 一、JSONObject和JSONArray的數(shù)據(jù)表示形式

JSONObject的數(shù)據(jù)是用 {  } 來表示的,

        例如:   { "id" : "123", "courseID" : "huangt-test", "title" : "提交作業(yè)", "content" : null  } 

而JSONArray,顧名思義是由JSONObject構(gòu)成的數(shù)組,用  [ { } , { } , ......  , { } ]  來表示

       例如:   [ {  "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ù)組來對(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é)

相關(guān)文章

最新評(píng)論