SoFunction
Updated on 2025-03-04

Detailed explanation of the differences and basic usage of JSONObject and JSONArray

1. Data representation form of JSONObject and JSONArray

The data of JSONObject is represented by {  }.

For example:{ "id" : "123", "courseID" : "huangt-test", "title" : "submit job", "content" : null  }

JSONArray, as the name suggests, is an array composed of JSONObject, represented by [ { } , { } , ......  , { } ]

For example:[ {  "id" : "123", "courseID" : "huangt-test", "title" : "submit job" }  ,  {  "content" : null, "beginTime" : 1398873600000  "endTime" } ] ;

Represents a JSONArray containing 2 JSONObjects.

You can see a very obvious difference, one uses the outermost one is {  }  and the other uses the outermost one is [  ] ;

2. How to obtain JSONObject object and JSONArray object from string String         

JSONObject jsonObject = new JSONObject ( String str);
     JSONArray jsonArray = new JSONArray(String  str ) ;

3. How to obtain JSONObject object from JSONArray

You can treat JSONArray as a general array, but the methods of obtaining data are different.

JSONObject  jsonObject = (i) ;  

4. Get data in JSON 

 int  mid=  ( "id" ) ;  // The data obtained by mid here is 123.   String mcourse=( " courseID") ;  // HeremcourseThe data obtained ishuangt-test.

Summarize