JAVA中快速解析JSON对象里包含的JSON数组
例如现在有这样一个Json
String Value = {"data":[{"school_name":"西北农林科技大学","school_id":"8"},{"school_name":"西北大学","school_id":"6"},
{"school_name":"西北工业大学","school_id":"5"},{"school_name":"西北政法大学","school_id":"2"},{"school_name":"西安交通大学","school_id":"4"},{"school_name":"西安建筑科技大学","school_id":"10"},{"school_name":"西安电子科技大学","school_id":"3"},{"school_name":"西安邮电大学","school_id":"1"},{"school_name":"长安大学","school_id":"9"},{"school_name":"陕西师范大学","school_id":"7"},{"school_name":"陕西科技大学","school_id":"11"}],"msg":"列表获取成功","status":0}
用2个数组接收school_name和school_id
首先
public String[] schoolID;
public String[] schoolName;
JSONObject jsonObj_school;//用来接收解析的JSON字符串
JSONArray jsonArr_school ;//用来接收JSON对象里的数组
try{
jsonObj_school = new JSONObject ( Value ); //解析JSON字符串
jsonArr_school =jsonObj_school.getJSONArray("data");//接收JSON对象里的数组
int jsonSize_school = jsonArr_school.length();.//获取数组长度
schoolID = new String[ jsonSize_school ];//初始化数组
schoolName = new String[ jsonSize_school];//初始化数组
for(int i = 0; i < jsonSize_school; i++ )//通过循环取出数组里的值
{
JSONObject jsonTemp = (JSONObject)jsonArr_school.getJSONObject(i);
schoolID[i] = jsonTemp.getString("school_id");
schoolName[i] = jsonTemp.getString("school_name");
}
}
catch(Exception e){
e.printStackTrace();
}
更多推荐
所有评论(0)