最近工作中遇到了json字符串解析的問題,但是項目中是有限制的,只能引用阿里巴巴和simple jar,所以我選擇了Alibaba的jar,由於很長時間沒用過json的東西了,有些淡忘,不免工作中遇到了會花費些許時間再去重拾記憶,所以寫了一個簡單的JSON 字符串數組的解析實例,僅此作為筆記,以后遇到了可以直接看一下,或許可以節省一時片刻的時間,時間就是金錢,就是生命,就是自信,就是信任...
/**
*
*/
package com.test.pac1;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.test.pac1.bean.PiProfitParamTo;
/**
* @author Administrator
*
*/
public class TestJson {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "[{\"id\":\"0377\",\"city\":\"平頂山\",\"isOld\":\"0\"},{\"id\":\"0377\",\"city\":\"南陽\",\"isOld\":\"1\"}]";
List<PiProfitParamTo> addList = new ArrayList<PiProfitParamTo>();
List<PiProfitParamTo> updateList = new ArrayList<PiProfitParamTo>();
JSONArray json = JSON.parseArray(s);
Iterator it = json.iterator();
while(it.hasNext()){
JSONObject sObj = (JSONObject)it.next();
String id = (String)sObj.get("id");
String city = (String)sObj.get("city");
Integer isOld = (Integer)sObj.get("isOld");
System.out.println("ID:" + id);
System.out.println("City:" + city);
System.out.println("isOld:" + isOld);
PiProfitParamTo pObj = new PiProfitParamTo();
if(isOld == 0)addList.add(pObj);
if(isOld != 0)updateList.add(pObj);
}
}
}