json传参
方式一
encodeURIComponent(appop);前端编码
//节点结构体 (对象封装)
function createObj(id, name, pId) {
this.id = id;
this.name = name;
this.pId = pId;
}
createObj.prototype.sayId = function() {
alert(this.id);
}
createObj.prototype.sayName = function() {
alert(this.name);
}
createObj.prototype.saypId = function() {
alert(this.pId);
}
//向后台提交选中的树节点信息
function fff() {
var treeObj = $.fn.zTree.getZTreeObj("treeSelect");
nodes = treeObj.getCheckedNodes(true);
var ss = new Array();//创建list集合
//var list= [];
v = "";
for (var i = 0; i < nodes.length; i++) {
var person = new createObj(nodes[i].id, nodes[i].name, nodes[i].pId);
ss[i] = person;
//v+="name:"+ nodes[i].name + ",";
//alert(nodes[i].id); //获取选中节点的值
//v+="id:"+ nodes[i].id + ",";
//v+="PId:"+ nodes[i].pId + ",";
//console.log(nodes);
}
//console.log(ss);
var appop = JSON.stringify(ss);
console.log(appop);
var jsonStrings = encodeURIComponent(appop);
console.log(jsonStrings);
$.ajax({
url : "../User/insertPermissions?jsonStrings="
+ jsonStrings,
type : "post",
dataType : "json",
async : false,
contentType : 'application/x-www-form-urlencoded;charset=UTF-8',
success : function(data) {
alert("提交成功");
console.log(data);
},
error : function(msg) {
alert("ajax连接异常:" + msg);
}
});
后端返回json格式的字符串,前台使用js中的JSON.parse()方法,把json字符串解析成java对象
后端接收
/*
* 展示提交的树节点信息
*/
@RequestMapping(value = "/insertPermissions", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
public Map<String, String> insertPermissions(@RequestParam(value = "jsonStrings") String jsonStrings,
HttpServletRequest request) throws IOException {
URLDecoder.decode(jsonStrings, "UTF-8");
List<Trees> trees = new ArrayList<Trees>();
JSONArray jsonArray = JSONArray.fromObject(jsonStrings);
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Trees trees2 = (Trees) JSONObject.toBean(jsonObject, Trees.class);
trees.add(trees2);
}
System.out.println(trees);
for (Trees tr : trees) {
System.out.println(tr.getName());
}
Map<String, String> map = new HashMap<String, String>();
map.put("result", "true");
return map;
}
方式二
前端转义处理
JSON.parse(jsonstr); //可以将json字符串转换成json对象
JSON.stringify(jsonobj); //可以将json对象转换成json对符串
var jsonArrayFinal = JSON.stringify(jsonArray);进行转换后再传递
前台按类的格式传递JSON对象:
var jsonUserInfo = "{\"TUserName\":\"" + userName + "\",\"TInterest\":\"" + interest + "\",\"TSex\":\"" + sex + "\",\"TCity\":\"" + city + "\",\"TDetail\":\"" + detail + "\"}";如拼出的jsonUserInfo 无转义符号,需要var jsonArrayFinal = JSON.stringify(jsonArray);进行转换后再传递
$.ajax(
{
type: "post",
url: "ReceiveHandler1.ashx",
data: { userInfo: jsonUserInfo, flag: "123456", key: "654321" },
dataType: "JSON",
success: function(data) {
$("#divShow").html(data);
}
});
后端解析
JSONObject、JSONArray。
JSONObject转化的应该是对象格式,
JSONArray转换的是数组对象(带[]形式的json)
//参数如: obj={data:[{"id":"","infoName":"11","infoContent":"11"},{"id":"","infoName":"22","infoContent":"22"}]}
// 解析接收的JSON字符串
JSONObject jsonObject = new JSONObject(obj);
// 接收JSON对象里的数组
JSONArray jsonArray = jsonObject.getJSONArray("data");
// 获取数组长度
int len = jsonArray.length();
// 通过循环取出数组里的值
for (int a = 0; a < len; a++)
{
JSONObject info = (JSONObject) jsonArray.getJSONObject(a);
String id = info.getString("id");
String infoName = info.getString("infoName");
String infoContent = info.getString("infoContent");
}
后台处理json格式数据
package jsonCh;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class jsonTest {
public static void main(String[] args) {
//1.比如从前端接收的是这个样子的json字符串,但是我们是不能直接获取到name sex phone所对应的值的,所以必须要对这个字符串进行解析,
String stu = "{\"name\":\"ss\",\"sex\":\"1\",\"phone\":\"123456789\"}";
//先转换成JSONObject类型
JSONObject stuObj = JSON.parseObject(stu);
//通过JSONObject中的getString("key")方法,得到对应的值
System.out.println("name:"+stuObj.getString("name")+" sex:"+stuObj.getString("sex"));
//2.字符串中含有数组的,比如像下面这个字符串
String stuInfo = "{\"classId\":1,\"stuInfo\":[{\"name\":\"aa\",\"sex\":\"1\",\"phone\":\"123456789\"},{\"name\":\"bb\",\"sex\":\"0\",\"phone\":\"987654321\"}]}";
JSONObject stuInfoObj = JSONObject.parseObject(stuInfo);//先转换成JSONObject类型
System.out.println("班级id:"+stuInfoObj.getString("classId"));//拿到班级id
String stuInfoStr = stuInfoObj.getString("stuInfo");//拿到stuInfo这个json字符串
System.out.println("学生信息集合:"+stuInfoStr);
JSONArray jsonstuInfo = JSONObject.parseArray(stuInfoStr);//将stuInfo解析成json数组
for (int i = 0; i < jsonstuInfo.size(); i++) {//遍历学生信息
JSONObject jsonStuInfo = jsonstuInfo.getJSONObject(i);//根据下标以此拿数据,每一个数据又是一个JSONObject对象,所以用JSONObject接收
String name = jsonStuInfo.getString("name");
String sex = jsonStuInfo.getString("sex");
String phone = jsonStuInfo.getString("phone");
System.out.println("这个学生的姓名是:"+name+"性别是:"+sex+"电话号码是:"+phone);
}
}
}