通过JSONObject解析
private String parseJSONWithJSONObject(String string) {
String returnData = "";
try {
JSONArray jsonArray = new JSONArray(string);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonCbject = jsonArray.getJSONObject(i);
String id = jsonCbject.getString("id");
String name = jsonCbject.getString("name");
String version = jsonCbject.getString("name");
returnData += id + name + version;
}
} catch (JSONException e) {
e.printStackTrace();
}
return returnData;
}
通过GSON解析
//新建APP类
public class APP {
private int id;
private String name;
private String version;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
private Object parseJSONWithGSON(String string) {
Gson gson = new Gson();
//借助 TypeToken 将期望解析成的数据类型传入到 fromJson()方法中
List<APP> appList = gson.fromJson(string, new TypeToken<List<APP>>() {}.getType());
for (APP app : appList) {//foreach循环
Log.d("MainActivity", "id is " + app.getId());
Log.d("MainActivity", "name is " + app.getName());
Log.d("MainActivity", "version is " + app.getVersion());
}
return null;
}