// 引入相应的包
//json-lib-2.2-jdk15.jar
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
1. //把java对象列表转换为json对象数组,并转为字符串
JSONArrayarray = JSONArray.fromObject(userlist);
Stringjsonstr = array.toString();
2.//把java对象转换成json对象,并转化为字符串
JSONObject object =JSONObject.fromObject(invite);
Log4jInit.ysulogger.debug(object.toString());
3.//把JSON字符串转换为JAVA对象数组
String personstr =getRequest().getParameter("persons");
JSONArray json =JSONArray.fromObject(personstr);
List<InvoidPerson>persons =(List<InvoidPerson>)JSONArray.toCollection(json,nvoidPerson.class);
4.//把JSON字符串转换为JAVA对象
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\"}]}";
JSONObjectjsonobject = JSONObject.fromObject(str);
PassportLendsEntitypassportlends = null;
try {
//获取一个json数组
JSONArrayarray = jsonobject.getJSONArray("passports");
//将json数组 转换成List<PassPortForLendsEntity>泛型
List<PassPortForLendsEntity>list = newArrayList<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);
//个人实例
前端jsp封装json字符串:
function subExa(){
var jsonstr = "{jsonstr :[";
$("select").each(function(){
var jsons = "{'exaid':"+$(this).attr("name")+",'exaval':"+$(this).val()+"},";
jsonstr = jsonstr + jsons;
});
jsonstr = jsonstr.substring(0,jsonstr.length-1);
jsonstr = jsonstr + "]}";
$.post("<c:url value='/vote/subExaSelscore.do'></c:url>",
// eval("("+jsonstr+")"),
{
jsonstr :jsonstr
},
function(data) {
});
{jsonstr :[{'exaid':28,'exaval':1},{'exaid':29,'exaval':1},{'exaid':89,'exaval':1},{'exaid':94,'exaval':1},{'exaid':86,'exaval':1},{'exaid':87,'exaval':1},{'exaid':88,'exaval':1},{'exaid':91,'exaval':1},{'exaid':84,'exaval':1},{'exaid':85,'exaval':1},{'exaid':92,'exaval':1},{'exaid':93,'exaval':1}]}
后台处理:
public String subExa(){
AppUser user = ContextUtil.getCurrentUser();
String jsonstr = getRequest().getParameter("jsonstr");
JSONObject jsonobject = JSONObject.fromObject(jsonstr);
JSONArray array = jsonobject.getJSONArray("jsonstr");
for (int i = 0; i < array.size(); i++) {
JSONObject object = (JSONObject)array.get(i);
Selscore exascore = new Selscore();
String exaid = object.getString("exaid");
String exaval = object.getString("exaval");
exascore.setScoreItem(new Long(exaid));
// exascore.setEvaluationId(user.getUserId());
exascore.setScoreScore(exaval);
selscoreService.save(exascore);
}
System.out.println();
return SUCCESS;
}