这是有效但奇怪的 json 响应。
当你解析这个 json 时,你会得到 27 个值。
14、三星、1、15、诺基亚……
但似乎每 3 个项目都是一个对象
“14,三星,1”
“15,诺基亚,1”。
而那个json并没有反映这一点。
使用这样的在线 json 解析器,你可以看到你能得到什么结果:
http://jsonviewer.stack.hu/
你可以像这样解析那个json:
List<String> list = new ArrayList<String>();
int counter = 0;
boolean first = true;
String json = "[\"14\",\"Samsung\",\"1\",\"15\",\"Nokia\",\"1\",\"16\",\"Sony Ericson\",\"1\",\"18\",\"LG\",\"1\",\"19\",\"Iphone\",\"1\",\"21\",\"HTC\",\"1\",\"22\",\"Motorola\",\"1\",\"23\",\"Micromax\",\"1\",\"41\",\"BlackBerry\",\"1\"]";
JSONArray getJSONArray;
try {
getJSONArray = new JSONArray(json);
for (int i = 0; i < getJSONArray.length(); i++) {
Log.d("", getJSONArray.getString(i) + "-" + i % 1 + "-" + i % 2);
if ((first == true && counter == 1) || (first == false && counter == 2)) {
list.add(getJSONArray.getString(i));
counter = 0;
first = false;
}
else {
counter += 1;
}
}
}
catch (JSONException e) {
e.printStackTrace();
}
记录您的阵列以查看您得到了什么:
for (String item : list) {
Log.d("list", item);
}
结果:
D/list ( 1669): Samsung
D/list ( 1669): Nokia
D/list ( 1669): Sony Ericson
D/list ( 1669): LG
D/list ( 1669): Iphone
D/list ( 1669): HTC
D/list ( 1669): Motorola
D/list ( 1669): Micromax
D/list ( 1669): BlackBerry
对于服务器站点:
通常json必须是这样的..
[
{
id: "14",
brand: "Samsung",
status: "1"
},
{
id: "15",
brand: "Nokia",
status: "1"
},
{
id: "16",
brand: "Sony Ericson",
status: "1"
}
]