听到我的JSON响应,我想要总清单价格。使用改进框架来解析JSON。
{
"price": [
"11",
"29"
]
}
答案 0 :(得分:0)
int sum = 0;
int price = gridValues.get(position).getPrice().size();
for (int j = 0; j < price; j++) {
String total =gridValues.get(position).getPrice().get(j);
sum += Integer.parseInt(total);
}
从列表中获取数据并在适配器中设置,并且在上面我将上面的代码放在上面并解决了我的问题
答案 1 :(得分:0)
我相信您的请求返回类型为JsonObject
。要获得price
JsonArray
的总和,您需要遍历它的每个对象并对其求和。
JsonObject object = parse.parse(data).getAsJsonObject();
JsonArray priceArray = object.getAsJsonArray("price");
int total = 0;
for (int i = 0; i < priceArray.size(); i++) {
total += priceArray.get(i).getAsInt();//reading (as int) each object of array
}
System.out.println("Total is " + total);