Android解析Json,由于Json构造不一样,解析方式也不一样
第一种JSON格式:
import org.json.JSONArray; import org.json.JSONObject; //jsonData的数据格式:[{ "id": "27JpL~jd99w9nM01c000qc", "version": "abc" },{ "id": "27JpL~j6UGE0LX00s001AH", "version": "bbc" },{ "id": "27JpL~j7YkM0LX01c000gt", "version": "Wa_" }] JSONArray arr = new JSONArray(jsonData); for (int i = 0; i < arr.length(); i++) { JSONObject temp = (JSONObject) arr.get(i); String id = temp.getString("id"); String id = temp.getString("version"); }
第二种JSON格式:
{"data": {"info":[ {"id":"01","name":"张三","年龄":"22","地址":"成都"}, {"id":"02","name":"李四","年龄":"23","地址":"北京"}, {"id":"03","name":"王五","年龄":"24","地址":"西安"}, {"id":"04","name":"赵六","年龄":"25","地址":"上海"}, {"id":"05","name":"周七","年龄":"26","地址":"深圳"} ] } }
JSONObject jsonObject = new JSONObject(result).getJSONObject("data"); JSONArray jsonArray=jsonObject.getJSONArray("info"); for(int i=0;i<jsonArray.length();i++){ JSONObject jo = (JSONObject)jsonArray.opt(i); ss[i]=jo.getInt("id")+" "+jo.getString("name")+" "+jo.getString("地址"); }