1.json的格式,有两种:

{"key": "value"} //JSONObject(对象)

[{"key1": "value1"}, {"key2": "value2"}] //JSONArray(数组)

2.json的遍历

假如json字符串如下:

{

"feature":"fresh_today",

"photos":[

{

"id":40581634,

"name":"Dandelion 5"

},

{

"id":40581608,

"name":"Dandelion 3"

}

]

}
可以看出来,最外面是个json对象,photos节点是个数组,遍历代码如下:

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

JSONObject jsonObject = JSONObject.fromObject(result);

String feature = jsonObject.getString("feature");

JSONArray photoArray = jsonObject.getJSONArray("photos");

for (int i = 0; i < photoArray.size(); i++) {

JSONObject object = (JSONObject) photoArray.get(i);

//或者用如下代码获取JSONObject

//JSONObject object = JSONObject.fromObject(photoArray.get(i));

int id = object.getInt("id");

String name = object.getString("name");

}

附上一个很好用的在线json格式化和校验的网站:http://jsonformatter.curiousconcept.com/