JSONObject和JSONArray的区别及基本用法
2017-04-09 11:41
801 查看
一、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 = jsonArray.getJSONObject(i) ;
四、获取JSON内的数据
int mid= jsonObject.getInt ( "id" ); // 这里的mid得到的数据就是123.
String mcourse=jsonObject.getString( " courseID"); // 这里的mcourse得到的数据就是huangt-test.
的,
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 = jsonArray.getJSONObject(i) ;
四、获取JSON内的数据
int mid= jsonObject.getInt ( "id" ); // 这里的mid得到的数据就是123.
String mcourse=jsonObject.getString( " courseID"); // 这里的mcourse得到的数据就是huangt-test.
的,
相关文章推荐
- JSONObject和JSONArray区别及基本用法
- JSONObject和JSONArray区别及基本用法
- 详解JSONObject和JSONArray区别及基本用法
- JSON全面总结(3)之JSONObject和JSONArray区别及基本用法
- JSONObject和JSONArray区别及基本用法
- JSONObject和JSONArray区别及基本用法
- JSONObject与JSONArray用法和区别
- JSONObject和JSONArray区别及基本用法
- Java json基本用法(org.json.JSONObject/org.json.JSONArray)
- JSONObject与JSONArray的区别 解析json数据
- JAVA基础——JSONObject,JSONArray 的一些常见用法
- JSONObject和JSONArray的使用 以及JSONObject put,accumulate,element的区别
- JSONArray和JSONObject的区别
- Volley的三种基本用法StringRequest的Get和post用法以及JsonObjectRequest
- JSONObject和JSONArray的理解与用法
- JSONObject/JSONArray的区别
- JSONObject和JSONArray的区别
- 自己做得json测试类,包含JSONArray,JSONObject,Gson基本使用方法
- JSONObject和JSONArray之间的区别、用法
- Gson基本操作,JsonObject,JsonArray,String,JavaBean,List互转