使用json-lib遍历数组与对象(JSONArray与JSONObject)

18 七月 2010 | J2EE | Tags: , ,

使用json-lib遍历数组与对象

//遍历json数组
String json1 = "{data:[{name:'Wallace'},{name:'Grommit'}]}";
jsonObjSplit = new JSONObject(json1);
JSONArray ja = jsonObjSplit.getJSONArray("data");
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
System.out.println(jo.get("name"));
}
 
//JSONObject遍历json对象
String json2 = "{name:'Wallace',age:15}";
jsonObj = new JSONObject(json2);
 
for (Iterator iter = jsonObj.keys(); iter.hasNext();) {
String key = (String)iter.next();
System.out.println(jsonObj .getString(Key));


3 Responses to “ 使用json-lib遍历数组与对象(JSONArray与JSONObject) ”

  1. 小邪 says:

    沙发,o(* ̄▽ ̄*)ゞ 嘿嘿 ~

  2. 伊长宫 says:

    你好,看到你的这篇文章,试了试运行不过去,不知道这个小程序要用到哪些包。望指点迷津
    QQ:543170025
    或发送邮件至 yi_changgong@126.com
    谢谢。。

    • fatkun says:

      用到的是json-lib包哦。。
      http://json-lib.sourceforge.net/
      json-lib还需要以下依赖包:
      jakarta commons-lang 2.5
      jakarta commons-beanutils 1.8.0
      jakarta commons-collections 3.2.1
      jakarta commons-logging 1.1.1
      ezmorph 1.0.6

发表评论

电子邮件地址不会被公开。

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">