Android 解析 json

 

要解析的json數據:
 
[java] 
{"data": 
        {"info":[ 
                        {"id":"01","name":"張三","年齡":"22","地址":"成都"}, 
                        {"id":"02","name":"李四","年齡":"23","地址":"北京"}, 
                        {"id":"03","name":"王五","年齡":"24","地址":"西安"}, 
                        {"id":"04","name":"趙六","年齡":"25","地址":"上海"}, 
                        {"id":"05","name":"周七","年齡":"26","地址":"深圳"} 
                    ] 
        } 
    } 
 
解析json:
[java
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; 
    } 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。