zoukankan      html  css  js  c++  java
  • 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();
    }
  • 相关阅读:
    【计算机网络】网络地址转换NAT
    红黑树
    引用和取地址区别
    [网络编程] TCP、UDP区别以及TCP传输原理、拥塞避免、连接建立、连接释放总结
    操作系统大端模式和小端模式
    点乘和叉乘
    HMM模型
    Application_Start
    跨函数使用内存空间
    框架
  • 原文地址:https://www.cnblogs.com/zmc/p/3606562.html
Copyright © 2011-2022 走看看