---
title: "Json字符串解析"
author: "it_iweb"
date: "2014-11-11T12:51:00Z"
tags: []
slug: "json"
draft: false
lastmod: "2014-12-12T08:45:23Z"
id: 2165715
url: "https://it-iweb.iteye.com/blog/2165715"
---

**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**