json java typeof_JSON对象和字符串的收发(JS客户端用typeof()进行判断非常重要)

Ajax前台向后台传递对象:

数据准备

将js对象或者json对象转换为json字符串在Ajax传递,在后台中再将json字符串转换为json对象,再转换为java对象或在前端和后端构造一样的数据结构,直接用application/json内容体发送。

48304ba5e6f9fe08f3fa1abda7d326ab.png

1 var student={

2 name:"abc",

3 age:12,

4 no:"123"

5 };

6

7 console.log(student);

8 //将js对象转换为json字符串通过ajax传递,在后台中再将json字符串转换为json对象再转换为java对象

9

10 student = JSON.stringify(student);

48304ba5e6f9fe08f3fa1abda7d326ab.png

Ajax:前端发送

48304ba5e6f9fe08f3fa1abda7d326ab.png

1 $.ajax({

2 url : url,

3 type : "POST",

4 data : {

5 sendData:"传递下面的json字符串",

6 jsonStr: student

7 },

8 async : isAsync,

9 dataType:data_type,

10 beforeSend : beforeSendFun,

11 success : function(return_data) {

12 successFun(return_data);

13 },

14 error : function(XMLHttpRequest, textStatus, errorThrown) {

15 alert("请求处理失败");

16 }

17 });

48304ba5e6f9fe08f3fa1abda7d326ab.png

后台解析:

48304ba5e6f9fe08f3fa1abda7d326ab.png

String sendData = request.getParameter("sendData");

if (sendData.equals("传递下面json字符串")){

String jsonStr = request.getParameter("jsonStr");

JSONObject student_json= new JSONObject().fromObject(jsonStr);//将json字符串转换为json对象

Student student = (Student)JSONObject.toBean(student_json,Student.class);//再将json对象转换为Student对象

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

(1)发端是对象var abc = new { name = "菜鸟教程", site = "http://www.runoob.com" };JSON(abc)

console.log(ds);

console.log(typeof (ds));object

console.log(ds.name);菜鸟教程

Objectname: "菜鸟教程"site: "http://www.runoob.com"__proto__: Object

ThemeList.js:24 {"name":"菜鸟教程","site":"http://www.runoob.com"}

ThemeList.js:27 "{\"name\":\"菜鸟教程\",\"site\":\"http://www.runoob.com\"}"

ThemeList.js:28 "\"{\\\"name\\\":\\\"菜鸟教程\\\",\\\"site\\\":\\\"http://www.runoob.com\\\"}\""

(2)发端是字符串  string abc= "{\"name\":\"菜鸟教程\",\"site\":\"http://www.runoob.com\"}";JSON(abc)

var mm=JSON.parse("\"{\\\"name\\\":\\\"菜鸟教程\\\",\\\"site\\\":\\\"http://www.runoob.com\\\"}\"")

console.log(typeof (mm));string

var ll = JSON.parse(mm);object

console.log(ll.name);菜鸟教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值