最近工作中遇到了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); } }}