当前位置:编程学习 > JS >>

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=易做图yticJson(s); 
        ArrayAdapter aa=new ArrayAdapter<String>(this,android.R.layout.易做图_list_item_1,ss);   
        listview.setAdapter(aa); 
    } 
 
    public String[] 易做图yticJson(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; 
    } 
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,