在做项目时,从网络获取的数据是json格式,需要进行解析,服务器返回的数据是整体无序,局部有序的,一开始有点慌,因为只知道有序的解析方式,后来查看API,可以先将jsondata转化封装成JSONBObject,然后再把有序部分封装成JSONArray。
jsondata如下:
具体解析过程如下:
private void parseJSONWithJSONObject(String jsondata){
try {
JSONObject jsonObject0 = new JSONObject(jsondata);
JSONArray jsonArray = jsonObject0.getJSONArray("data");
for(int i = 0; i < jsonArray.length(); i++)
{
JSONObject jsonObject = jsonArray.getJSONObject(i);
String cityId = jsonObject.getString("CityID");
String cityName = jsonObject.getString("CityName");
String spotId = jsonObject.getString("SpotID");
String spotName = jsonObject.getString("SpotName");
String spotPicUrl = jsonObject.getString("SpotPicUrl");
int countGood = jsonObject.getInt("CountGood");
int countLoveLocks = jsonObject.getInt("CountLoveLocks");
int CountBless = jsonObject.getInt("CountBless");
int CountWish = jsonObject.getInt("CountWish");
Log.i("jsonToData cityId",cityId);
Log.i("jsonToData cityName",cityName);
Log.i("jsonToData spotName",spotName);
Log.i("jsonToData spotPicUrl",spotPicUrl);
Log.i("jsonToData countGood",""+countGood);
Log.i("jsonToData countLoveLocks",""+countLoveLocks);
Log.i("jsonToData CountBless",""+CountBless);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}