`
it_iweb
  • 浏览: 9495 次
  • 性别: Icon_minigender_1
  • 来自: 天津
文章分类
社区版块
存档分类
最新评论

Json字符串解析

 
阅读更多

Json字符串解析:

package csdm;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

public class JsonReader {
 public static void main(String[] args) {
  try {
   String result = "{'id':{'a':'0','b':'1'},'users':[{'toid':1,'tosex':2,'realname':'zs'},{'toid':44,'tosex':55,'realname':'zs'}]}";

   JSONObject json = new JSONObject(result);
   System.out.println("取id值:" + json.get("id"));
   System.out.println("取id中a值:" + json.getJSONObject("id").get("a"));
   System.out.println("取id中b值:" + json.getJSONObject("id").get("b"));
   JSONArray jsonArray = json.getJSONArray("users");
   int iSize = jsonArray.length();
   System.out.println("json中数组Size:" + iSize);
   for (int i = 0; i < iSize; i++) {
    JSONObject jsonObj = jsonArray.getJSONObject(i);
    System.out.println("[" + i + "]toid=" + jsonObj.get("toid"));
    System.out.println("[" + i + "]tosex=" + jsonObj.get("tosex"));
    System.out.println();
   }
  } catch (JSONException e) {
   e.printStackTrace();
  }
 }
}
/**
* 输出结果 取id值:{"a":"0","b":"1"} 取id中a值:0 取id中b值:1 json中数组Size:2 [0]toid=1
* [0]tosex=2
* 
* [1]toid=44 [1]tosex=55
*/



jar 包下载链接:http://www.java2s.com/Code/Jar/o/Downloadorgcodehausjettisonjar.htm

jar api文档链接:http://www.boyunjian.com/javasrc/org.apache.servicemix.bundles/org.apache.servicemix.bundles.jettison/1.0.1_5/_/org/codehaus/jettison/json/JSONObject.java

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics