WEB开发网
开发学院手机开发Android 开发 Android 解析 json 阅读

Android 解析 json

 2012-09-15 10:10:20 来源:WEB开发网   
核心提示: 要解析的json数据:{"data":{"info":[ {"id":"01","name":"张三","年龄":"22","地址

 

要解析的json数据:

{"data":
		{"info":[
		         		{"id":"01","name":"张三","年龄":"22","地址":"成都"},
		         		{"id":"02","name":"李四","年龄":"23","地址":"北京"},
		         		{"id":"03","name":"王五","年龄":"24","地址":"西安"},
		         		{"id":"04","name":"赵六","年龄":"25","地址":"上海"},
		         		{"id":"05","name":"周七","年龄":"26","地址":"深圳"}
			        ]
		}
	}

解析json:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listview=(ListView) findViewById(R.id.listView1);
        String [] ss=analyticJson(s);
        ArrayAdapter aa=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ss);  
        listview.setAdapter(aa);
    }

    public String[] analyticJson(String result){
		try {
			String[] ss=new String[5];//为了演示方便让其返回String[]
			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("地址");
			}
			return ss;
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}

Tags:Android 解析 json

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接