数据内容为JSONObject
/**
* 封装返回的信息
* @param statusCode 状态码
* @param msg 消息
* @param json 数据内容
* @return
*/
public static String writeMsg(int statusCode, String msg,JSONObject json) {
JSONObject jsonse = new JSONObject();
jsonse.put("statusCode", statusCode);
jsonse.put("msg", msg);
jsonse.put("data", json);
return jsonse.toString();
}
若向上述writeMsg方法传递的JSONObject样式如下
{
"主菜单1":[
{
"sourceUrl":"1-1-sourceUrl",
"name":"1-1-name"
}
],
"主菜单2":[
{
"sourceUrl":"2-1-sourceUrl",
"name":"2-1-name"
},
{
"sourceUrl":"2-2-sourceUrl",
"name":"2-2-name"
},
{
"sourceUrl":"2-3-sourceUrl",
"name":"2-3-name"
}
]
}
则在前端ajax异步请求的success回调函数中,可以按如下方式解析返回的JSONObject
JS中字符串与JSON对象的相互转换
将JSON字符串反序列化成JSON对象
JSON.parse(jsonstr);
将JSON对象序列化成JSON对符串
JSON.stringify(jsonobj);
$.post('provincecitycounty/getUserSelectedCity.do',
{namecode_city:namecode,city:city},
function(data) {
var json=JSON.parse(data);
if(json.statusCode==1){
$("#city_name").text(city);
}
});