【发布时间】:2013-06-27 00:43:44
【问题描述】:
我正在尝试解析我的 json:
for(int i = 0; i < json.getJSONArray("JSON").length(); i++) {
String taste = json.getJSONArray("JSON").getJSONObject(i).getString("taste");
String rate = json.getJSONArray("JSON").getJSONObject(i).getString("rate");
int foo = Integer.parseInt(rate);
count = count + foo;
//create object
BeerTastes tempTaste = new BeerTastes(taste, rate);
//add to arraylist
tasteList.add(tempTaste);
Log.d("taste",tempTaste.taste);
Log.d("number",tempTaste.percent);
}
但是我最后的日志根本没有输出任何东西,所以我认为我没有正确解析我的 json。我正在看的json是:
[{"taste":"Bitter","rate":"13"},{"taste":"Malty","rate":"3"},{"taste":"Smooth","rate":"3"},{"taste":"Dry","rate":"1"}]
我想我可能错了:
json.getJSONArray("JSON")
因为我的数组没有名称,但我必须带一个字符串...
【问题讨论】:
-
那是一个JSONArray。
-
我知道,这就是为什么我使用 getJSONArray() 不确定您要指出的内容。也许尝试更具体一点。
-
这是duplicate of this question。正如@SLaks 所暗示的,删除每个
.getJSONArray("JSON"),它应该可以工作。