json 的解析-使用Android中的JSONObject和JSONArray解析json数据,gson中的JsonReader解析json数据
2013-06-10 01:01
961 查看
关于Json:http://www.json.org/
Json解析库gson: http://code.google.com/p/google-gson/
1.使用Android中的JSONObject和JSONArray解析json数据
2.使用gson中的JsonReader解析json数据
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().equals("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 block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
相关文章推荐
- json 的解析-使用Android中的JSONObject和JSONArray解析json数据
- 关于SAXReader解析XML以及JSONObject和JSONArray的使用总结
- Android中重要数据解析JSON工具之gson库的使用
- Android下使用Gson解析JSON数据
- Android中使用Gson解析JSON数据的两种方法
- Android中使用Gson解析JSON数据的两种方法
- Android使用Gson解析JSON数据
- Android中使用Gson解析JSON数据的两种方法
- Android-json解析:原生JSONObject+JSONArray的使用
- Android使用Gson解析JSON数据
- Android中使用Gson解析JSON数据
- Android中使用Gson解析JSON数据的两种方法
- Android基础之使用Gson解析json数据 第一弹
- Android中使用Gson解析JSON数据
- Android中Json的全解析(JSONObject,JSONArray,Gson,Jackson)
- Android中Json数据解析(二)--使用Gson、Jackson和FastJson解析Json数据
- Android中使用Gson解析JSON数据的方法
- 在Android中可以使用Gson解析JSON数据
- Android中使用Gson解析JSON数据的两种方法
- Android中Gson解析json数据使用@SerializedName注解与java对象不匹配的字段