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

JSON数据的简单解析

运行结果:
 
 \
 
--------------------------------------
Java代码 
//解析方法 
    public void parseJson(String jsonData) { 
        try { 
            JsonReader reader = new JsonReader(new StringReader(jsonData)); 
            reader.beginArray(); 
            while (reader.hasNext()) { 
                reader.beginObject(); 
                while (reader.hasNext()) { 
                    String tagName = reader.nextName(); 
                    if (tagName.equals("name")) { 
                        System.out.print("name=" + reader.nextString() + "  "); 
                    } else if (tagName.equals("age")) { 
                        System.out.println("age=" + reader.nextInt()); 
                    } 
                } 
                reader.endObject(); 
            } 
            reader.endObject(); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
 
=============================================== 
public class Main extends Activity { 
    /** Called when the activity is first created. */ 
    private String jsonData = "[{\"name\":\"Jack\",\"age\":20},{\"name\":\"Tom\",\"age\":25}]"; 
 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        JsonUtils jsonUtils = new JsonUtils(); 
        jsonUtils.parseJson(jsonData); 
 
    } 

作者“Android开发”
 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,