前端之家收集整理的这篇文章主要介绍了
【第16篇】通过fastjson去解析json数组数据,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package ivyy.taobao.com.domain.fastjson;
import java.util.Iterator;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
* @Author:jilongliang
* @Date:2014-12-19
* @Version:1.0
* @Description:fastjson和json-lib,gson都支持’如:
*正确:jsonStr="[{\"age\":22,\"sex\":\"男\",\"userName\":\"xiaoliang\"},{\"age\":22,\"userName\":\"xiaoliang\"}]";
*正确:jsonStr="[{'age':22,'sex':'男','userName':'xiaoliang'},{'age':22,'userName':'xiaoliang'}]";
*/
public class JsonTest1 {
public static void main(String[] args) {
//[{"age":22,"sex":"男","userName":"xiaoliang"},{"age":22,"userName":"xiaoliang"}]
//jsonStr="[{'age':22,'userName':'xiaoliang'}]";
StringBuffer buff=new StringBuffer();
buff.append("[");
buff.append("{");
buff.append("'weixin'").append(":").append("'YFDSBUYI'").append(",");
buff.append("'weibo'").append(":").append("'http://weibo.com/resourceljl'").append(",");
buff.append("'qq'").append(":").append("'1302128216'").append(",");
buff.append("'email'").append(":").append("'buyee_hr@126.com'").append(",");
buff.append("'address'").append(":").append("'广东省云浮市云城区闻莺路39号'").append("");
buff.append("}");
buff.append(",");//第一个数组结尾
buff.append("{");
buff.append("'weixin'").append(":").append("'YFDSBUYI'").append(",");
buff.append("'address'").append(":").append("'广东省云浮市云城区闻莺路39号'").append("");
buff.append("}");
buff.append("]");
String jsonStr=buff.toString();
//转换成数组数据并遍历
JSONArray jarr=JSONArray.parseArray(jsonStr);//JSON.parseArray(jsonStr);
for (Iterator iterator = jarr.iterator(); iterator.hasNext();) {
JSONObject job=(JSONObject)iterator.next();
String weibo=job.get("weibo").toString();
System.out.println(weibo);
}
}
}
原文链接:https://www.f2er.com/json/289913.html