Dolphinsz 2013-06-24
package com.test;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JsonTest {
public static void main(String[]args){
JSONArray arr=new JSONArray();
JSONObject objtotal=new JSONObject();
for(int i=0;i<3;i++){
JSONObject obj=new JSONObject();
obj.put("name", "aa"+i);
obj.put("sex", "nan"+i);
obj.put("yuwen", "34"+i);
obj.put("shuxue", "99"+i);
arr.add(obj);
}
objtotal.put("insert", arr);
//将object转化成String类型
String objStr=objtotal.toString();
//将字符串转换成object类型
JSONObject strToObj=JSONObject.fromObject(objStr);
JSONArray arrP=objtotal.getJSONArray("insert");
for(int i=0;i<arrP.size();i++){
JSONObject objP=arrP.getJSONObject(i);
System.out.println(objP.get("name"));
System.out.println(objP.get("sex"));
System.out.println(objP.get("yuwen"));
System.out.println(objP.get("shuxue"));
System.out.println("第"+i+"数据");
}
System.out.println(objtotal);
}
}