待解析的JSON格式的文件如下:
[{"id":"5", "version":"1.0", "name":"xiaowang"},
{"id":"10", "version":"2.0", "name":"lisi"}]
一、使用JSONObject來解析JSON數據
官方提供的,所有不需要導入第三方jar包;直接上代碼,如下:
1 //方法一:使用JSONObject
2 private void parseJSONWithJSONObject(String JsonData) {
3 try
4 {
5 JSONArray jsonArray = new JSONArray(jsonData);
6 for (int i=0; i < jsonArray.length(); i++) {
7 JSONObject jsonObject = jsonArray.getJSONObject(i);
8 String id = jsonObject.getString("id");
9 String name = jsonObject.getString("name");
10 String version = jsonObect.getString("version");
11
12 System.out.println("id" + id + ";name" + name + ";version" + version);
13 }
14 }
15 catch (Exception e)
16 {
17 e.printStackTrace();
18 }
19 }
步驟解讀:
定義一個JSON數組,用於將服務器返回的數據傳入到一個JSONArray對象中; 然后循環遍歷這個JSONArray,
從中取出每一個元素(JSONObject對象),接下來只需調用getString()方法即可將數據取出。
二、使用GSON
使用該方法解析JSON數據,首先需要添加GSON的jar包;下載地址是:http://download.csdn.net/detail/a924571572/5824225
需要導入的jar包如圖: 
下面是核心代碼:
//方法二:使用GSON
private void parseJSONWithGSON(String JsonData) {
Gson gson = new Gson();
List<App> applist = gson.fromJson(jsonData,
new TypeToken<List<App>>() {}.getType());
for(App app : applist) {
System.out.println("id" + app.getId() + ";name" + app.getName() + ";version" + app.getVersion());
}
}
步驟解讀:
根據JSON數據內容需要定義一個類,用存放數據,如App類:
public class App {
private String id;
private String name;
private String version;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
//......
}
如只有一組數據,則可以直接調用如下代碼
GSON gson = new GSON(); App app = gson.fromJson(jsonData, App.class);
如果有多組數據,則需要借助TypeToken將期望解析成的數據類型傳入fromJson()方法中:
List<App> app = gson.fromJson(jsonData, new TypeToken<<List<App>>> ().getType());
之后直接使用App對象的方法,如:getId、getName....即可獲取數據
補充:
TypeToken是什么呢?
TypeToken的使用非常簡單,如上面的代碼,只要將需要獲取類型的泛型類作為TypeToken的泛型參數構造一個匿名的子類,就可以通過getType()方法獲取到我們使用的泛型類的泛型參數類型。
三、使用Jackson
第三方的工具知道該怎么處理了吧,jar包的下載地址:http://wiki.fasterxml.com/JacksonDownload
其中需要使用到:
jackson-databind.jar 核心包(必須),提供基於“流模式”解析的API【JsonPaser(json流讀取),JsonGenerator(json流輸出)】
jackson-annotations.jar 數據綁定包(可選),提供基於“對象綁定”和“樹模型”相關API。【ObjectMapper,JsonNode(樹節點)】
jackson-core.jar 注解包(可選),提供注解功能。
核心方法:
public static void parseJSONWithJackson(String jsonData) {
ObjectMapper mapper = new ObjectMapper();
try {
App app = mapper.readValue(jsonData, App.class);
System.out.println("id" + app.getId() + ";name" + app.getName() + ";version" + app.getVersion());
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
四、使用Fastjson
不多說,直接上jar包下載地址:http://download.csdn.net/download/finaljia/5293875
核心代碼:
JSONArray jarr = JSONArray.parseArray(jsonData); //JSON.parseArray(jsonStr);
for (Iterator iterator = jarr.iterator(); iterator.hasNext(); ) {
JSONObject job = (JSONObject)iterator.next();
String id = job.get("id").toString();
String name = job.get("name").toString();
String version = job.get("version").toString();
System.out.println("id" + id + ";name" + name + ";version" + version);
}
調用rest接口時,數據格式通常為json,為了增加代碼的可讀性和維護性,不建議直接操作JSONObject,而是轉換成實際的對象進行處理,遇到比較復雜的對象時,可以通過FastJson提供的TypeReference進行處理,Demo如下:
String jsonStr = HttpUtil.post(requestBody,remoteUrl);
Map<String,List<User>> resultMap = JSON.parseObject(jsonStr, new TypeReference<Map<String,List<User>>>(){});

