java如何使用GSON在Android中获取具有特定值的JSON对象
我想从字符串类型的JSON解析数据。我的JSON响应看起来像
[{"order":-1,"number":"5"},{"order":0,"number":"7"},{"order":1,"number":"3"}]
我试着使用下一种方法:
JSONArray json = new JSONArray("My JSON String");
for (int i = 0; i < json.length(); i++)
{
JSONObject json_staticon = json.getJSONObject(i);
if(json_staticon.getInt("order") == 0) {
return json_staticon.getInt("number");
}
}
但我想用Google GSON库解析我的JSON响应,所以。。。
有什么更好的方法可以使用GSON通过order = 0
获得对象中的number
值
谢谢
# 1 楼答案
使用Gson
秩序。爪哇
记录的值
也可以使用增强的for循环
for (Orders orders: details) {
来代替for (int i = 0; i <details.size(); i++) {