1.常見操作和方法
 
        1.1 對象之間的轉化
 
        // String對象到JSONObject對象
JSONObject dataJson = JSONObject.parseObject(data);
//String對象到JSONArray對象
JSONArray dataList = JSONObject.parseArray(data);
 
        2.操作實例
 
        2.1 實例1
 
        /**
 * 操作list格式的Json對象
 */
@Test
public void operateJsonList() throws Exception{
    //讀取數據
    File file = new File("src/main/java/fastjson/data/JsonData.json");
    String data = FileUtils.readFileToString(file, "UTF-8");
    //String  --> JSONObject
    JSONObject dataJson = JSONObject.parseObject(data);
    //獲取JSONObject中的list數據
    JSONArray jsonList = dataJson.getJSONObject("result").getJSONArray("rows");
    //遍歷JSONArray
    for(Iterator iterator = jsonList.iterator();iterator.hasNext();){
        JSONObject jsonObject = JSONObject.parseObject(iterator.next().toString());
        Long id = jsonObject.getLong("id");
        System.out.println(id);
    }
}
 
         
         
        2.2 實例2
 
        /**
 * 從JSONArray中篩選出特定的數據
 * 基本思路就是:把符合的數據保存到另一個數據容器中,然后再把該容器Json序列化即可
 */
@Test
public void saveDataFromJsonArray() throws Exception{
    File file = new File("src/main/java/fastjson/data/JsonArrayData.json");
    String data = FileUtils.readFileToString(file, "UTF-8");
    JSONArray dataList = JSONObject.parseArray(data);
    //新建數據容器
    List<JSONObject> result = new ArrayList(dataList.size());
    //篩選sex==1的數據
    for(Iterator iterator=dataList.iterator();iterator.hasNext();){
        JSONObject next = (JSONObject) iterator.next();
        Integer sex = next.getInteger("sex");
        if(sex.equals(1)){
            result.add(next);
        }
    }
    //把新的容器進行序列化
    dataList = JSONObject.parseArray(result.toString());
    System.out.println(dataList);
}