以上一篇博客为例:
volley学习
解析以下json数据:
{
"result": [
[
"电脑桌",
"2081633"
],
[
"电脑椅",
"967292"
],
[
"电脑主机",
"155392"
],
[
"电脑包",
"3275851"
],
[
"电脑音响",
"525074"
],
[
"电脑桌 台式 家用",
"294706"
],
[
"电脑耳机",
"407464"
],
[
"电脑显示器",
"210153"
],
[
"电脑桌 床上用",
"159902"
],
[
"电脑机箱",
"91282"
]
]
}
分析:这是一个对象里面嵌套一个数组,数组里面又是几个数组,只是这些元素没有名字。
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(URL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
try {
StringBuffer sb = new StringBuffer();
JSONObject jsonObject1 = new JSONObject(jsonObject.toString());
JSONArray jsonArray = jsonObject1.getJSONArray("result");
for(int i=0;i<jsonArray.length();i++){
JSONArray ja = jsonArray.getJSONArray(i);
for(int j=0;j<ja.length();j++){
Log.i("------------",(String)ja.get(j));
sb.append(ja.get(j));
}
sb.append("\n");
}
text.setText(sb);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
text.setText("Json Error");
}
});
结果:
结语
是不是写得非常简单?的确,本来就很简单。