JSONObject是包含在 { } 里面的数据集合 如: { "id" : "123", "courseID" : "test", "title" : "测试", "content" : null }
而JSONArray呢,则为JSONObject的集合,即一个包含多个JSONObject的多维数组,表达形式为 [ { } , { } , ...... , { } ] 如: [ { "id" : "123", "courseID" : "test", "title" : "测试" } , { "content" : null, "beginTime" :" 20180504", "endTime":"20180601" } ]
它们的操作主要有两个,一个是从字符串String获得数据
JSONObject jsonObject = new JSONObject (String str); // 获取JSONObject对象 JSONArray jsonArray = new JSONArray(String str) ; // 获取JSONArray对象 JSONObject jsonObject = jsonArray.getJSONObject(i) ; // 从JSONArray中获得JSONObject对象
再一个就是获取JSON中的数据
int theid= jsonObject.getInt ( "id" ) ; // 获取数值数据,这里的mid得到的数据就是123. String thestr=jsonObject.getString( "courseID") ; // 获取字符串数据,这里的thestr得到的数据就是test.其实用得更多的还是这个
类别