使用官方提供的JSONObject
- 将返回的json数据放进一个JSONArray中
- 遍历这个Array,得到JSONArray.getJSONObject(i)方法得到每一个JSONObject对象
- 利用JSONObject对象的getString(“id”)方法获取具体的内容
public void parseJSONWithJSONObject(String jsonData){
try{
JSONArray jsonArray = new JSONArray(jsonData);
for(int i = 0;i<jsonArray.length();i++){
JSONObject jsonObject= jsonArray.getJSONObject(i);
String id = jsonObject.getString("id");
}
}catch(Exception e){
e.printStackTrace();
}
}
使用谷歌提供的GSON
- build.gradle中添加对GSON的依赖
implementation 'com.google.code.gson:gson:2.7'
- 新建一个和JSON文件中的对象相对应的类
```
Gson gson = new Gson();
List<App> appList = gson.fromJson(jsonData,new TypeToken<List<App>>(){}.getType());
或者
App app1 = gson.fromJson(jsonData,App.class());
```