JSONObject和JSONArray

1、JSONObject和JSONArray的數據表示形式json

JSONObject的數據是用 {  } 來表示的,數組

        例如:   { "id" : "123", "courseID" : "huangt-test", "title" : "提交做業", "content" : null  }  .net

而JSONArray,顧名思義是由JSONObject構成的數組,用  [ { } , { } , ......  , { } ]  來表示對象

       例如:   [ {  "id" : "123", "courseID" : "huangt-test", "title" : "提交做業" }  ,  {  "content" : null, "beginTime" : 1398873600000  "endTime" } ] ; blog

        表示了包含2個JSONObject的JSONArray。字符串

能夠看到一個很明顯的區別,一個最外面用的是 {  }  ,一個最外面用的是 [  ]  ;get

 

2、如何從字符串String得到JSONObject對象和JSONArray對象it

          JSONObject  jsonObject  = new JSONObject ( String  str);test

         JSONArray jsonArray = new JSONArray(String    str  ) ;方法

 

3、如何從JSONArray中得到JSONObject對象

       你們能夠把JSONArray當成通常的數組來對待,只是獲取的數據內數據的方法不同

                   JSONObject   jsonObject  =  jsonArray.getJSONObject(i) ;    

 

4、獲取JSON內的數據

      int   mid= jsonObject.getInt ( "id" ) ;    //  這裏的mid獲得的數據就是123.

      String  mcourse=jsonObject.getString( " courseID") ;   // 這裏的mcourse獲得的數據就是huangt-test.

 

http://blog.csdn.net/greatsuny/article/details/39288355 

http://blog.csdn.net/lishuangzhe7047/article/details/28880009

相關文章
相關標籤/搜索