Android中解析Json方法
2011-10-25 10:29
453 查看
[转载]解析json – penink – 博客园.
关于Json:http://www.json.org/
Json解析库gson: http://code.google.com/p/google-gson/
1.使用Android中的JSONObject和JSONArray解析json数据
2.使用gson中的JsonReader解析json数据
关于Json:http://www.json.org/
Json解析库gson: http://code.google.com/p/google-gson/
1.使用Android中的JSONObject和JSONArray解析json数据
String strJson = "{\"students\":[{\"name\":\"Jack\",\"age\":12}, {\"name\":\"Vista\",\"age\":23}, {\"name\":\"Kaka\",\"age\":22}, {\"name\":\"Hony\",\"age\":31}]}";
try {JSONObject jo = new JSONObject(strJson);
JSONArray jsonArray = (JSONArray) jo.get("students");
for (int i = 0; i < jsonArray.length(); ++i) {
JSONObject o = (JSONObject) jsonArray.get(i);
System.out.println("name:" + o.getString("name") + "," + "age:"+ o.getInt("age"));
}} catch (JSONException e) {
e.printStackTrace();
}
2.使用gson中的JsonReader解析json数据
try {
String string = "{\"class\":1, \"students\":[{\"name\":\"jack\", \"age\":21},{\"name\":\"kaka\", \"age\":21},{\"name\":\"lucy\", \"age\":21}]}";
StringReader sr = new StringReader(string);
JsonReader jr = new JsonReader(sr);
jr.beginObject();
if (jr.nextName().contains("class")) {
System.out.println("班级: " + jr.nextString());
if (jr.nextName().equals("students")) {
jr.beginArray();
while (jr.hasNext()) {
jr.beginObject();
if (jr.nextName().equals("name"))
System.out.print("姓名:" + jr.nextString());
if (jr.nextName().equals("age")) {
System.out.println(" , 年龄:" + jr.nextInt());}jr.endObject();
}
jr.endArray();
}}
jr.endObject();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch
blocke.printStackTrace();
} catch (IOException e) {// TODO Auto-generated catch
blocke.printStackTrace();
}
相关文章推荐
- JSON复杂文档解析 Android自带JSONObject,JSONArray方法
- Android网络开发中如何使用JSON进行网络通信---Android_JSON数据通讯方法解析
- Android中使用Gson解析JSON数据的方法
- Android中解析JSON格式数据常见方法合集
- Android网络开发中如何使用JSON进行网络通信---Android JSON数据通讯方法解析
- Android中使用Gson解析JSON数据的两种方法
- Android开发:JSON简介及最全面解析方法(Gson、AS自带org.json、Jackson解析)
- Android中gson、jsonobject解析JSON的方法详解
- Android中对XML和JSON数据解析的方法汇总
- Android JSON原生解析的几种思路和GSON的使用方法
- Android编程简单解析JSON格式数据的方法示例
- Android中使用Gson解析JSON数据的两种方法
- Android App数据格式Json解析方法和常见问题
- Android 解析Json的各种方法
- android解析JSON字符串 (最强最简便方法)
- android中常见的二种数据解析方法----XML和Json
- JSON学习(案列):Android中解析JSON格式数据常见方法合集
- Android开发:JSON简介及最全面解析方法(Gson、AS自带org.json、Jackson解析)
- Android---3种Json解析方法+解析框架
- Android中使用Gson解析JSON数据的两种方法