//json-lib-2.2-jdk15.jar
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
一、JS操作
//JSON对象 var a={"name":"tom","sex":"男","age":"24"}; //JSON字符串 var b='{"name":"Mike","sex":"女","age":"29"}';
1.1.对象 转 字符串
var a={"name":"tom","sex":"男","age":"24"};
var objectToStr=JSON.stringify(a);
IE8 内置JSON.stringify,中文变unicode的问题
eval("var str = '"+JSON.stringify(objectToStr)+"';"); alert(str);
1.2.字符串 转 对象
var b='{"name":"Mike","sex":"女","age":"29"}'; var strToObj=JSON.parse(b);
或
var strToObj= eval('(' +b+ ')');
1.3.定义数组,添加对象
var json = {merchants:[]}; if (document.all.chk.length && document.all.chk.length != undefined) { for (i = 0; i < document.all.chk.length; i++) { if (document.all.chk[i].checked == true) { //checkbox的value //<INPUT type="checkbox" name="chk" value="{'id':'<s:property value="MERCH_ID"/>','name':'<s:property value="MERCH_NAME"/>'}" /> var tempStr = document.all.chk[i].value; tempStr = tempStr.replace(reg,"\""); var tempJson = JSON.parse(tempStr); json.merchants.push(tempJson); } } } else { if(document.all.chk.checked == true) { var tempStr = document.getElementsByName('chk')[0].value; tempStr = tempStr.replace(reg,"\""); var tempJson = JSON.parse(tempStr); json.merchants.push(tempJson); } }
1.4.遍历对象数组
for(var i=0;i<json.merchants.length;i++){
alert(json.merchants[i].id + "=" + json.merchants[i].name);
}
二、JAVA操作
//json-lib-2.2-jdk15.jar import net.sf.json.JSONArray; import net.sf.json.JSONObject;
2.1.java 对象列表转换为json对象数组,并转为字符串
JSONArray array = JSONArray.fromObject(userlist);
String jsonstr = array.toString();
2.2.java对象转换成json对象,并转化为字符串
JSONObject object = JSONObject.fromObject(user);
syso(object.toString());
2.3.json字符串转换为java对象数组
String usersStr = getRequest().getParameter("users"); JSONArray json = JSONArray.fromObject(usersStr); List<User> userList= (List<User>)JSONArray.toCollection(json, User.class);
2.4.json字符串转换为java对象
/** jionFormatStr= '{"lendperson":"李四","lendcompany":"有限公司","checkperson":"李四", "lenddate":"2010-07-19T00:00:00","lendcounts":4, "passports":[{"passportid":"d","name":"李豫川","passporttype":"K"}, {"passportid":"K9051","name":"李平","passporttype":"K"}, {"passportid":"K90517","name":"袁寒梅","passporttype":"K"}, {"passportid":"K905199","name":"贺明","passporttype":"K"}]}'; **/ str = "{\"lendperson\":\"李四\",\"lendcompany\":\"有限公司\",\"checkperson\":\"李四\", \"lenddate\":\"2010-07-19T00:00:00\",\"lendcounts\":4,\" passports\":[{\"passportid\":\"d\",\"name\":\"李豫川\",\"passporttype\":\"K\"}, {\"passportid\":\"K9051\",\"name\":\"李平\",\"passporttype\":\"K\"}, {\"passportid\":\"K90517\",\"name\":\"袁寒梅\",\"passporttype\":\"K\"}, {\"passportid\":\"K905199\",\"name\":\"贺明\",\"passporttype\":\"K\"}]}"; JSONObject jsonobject = JSONObject.fromObject(str); PassportLendsEntity passportlends = null; try { //获取一个json数组 JSONArray array = jsonobject.getJSONArray("passports"); //将json数组 转换成 List<PassPortForLendsEntity>泛型 List<PassPortForLendsEntity> list = new ArrayList<PassPortForLendsEntity>(); for (int i = 0; i < array.size(); i++) { JSONObject object = (JSONObject)array.get(i); PassPortForLendsEntity passport = (PassPortForLendsEntity)JSONObject.toBean(object, PassPortForLendsEntity.class); if(passport != null){ list.add(passport); } } //转换PassportLendsEntity 实体类 passportlends = (PassportLendsEntity)JSONObject.toBean(jsonobject, PassportLendsEntity.class); }