json前台后台相互转换

后台解析json: 用fastjson

1. JSON.parseObject: 将Json字符串转化为相应的对象
2. JSON.toJSONString: 将对象转化为Json字符串
如:

Staff staff = JSON.parseObject(jsonString, Staff.class); //json字符串转化为类
String jsonStr = JSON.toJSONString(staff); //类转化为json字符串

具体例子: https://blog.csdn.net/antony9118/article/details/71023009

==================================================================

前台解析json:

JSON对象包含两个方法: 

1. JSON.parse:用于解析JSON为对象

2. JSON.stringify:将对象/值转换为JSON字符串

用法如下

$.ajax({
  type : 'post',
  url : '/dyncContractSign',
  data: JSON.stringify({
   contractId: $("#contractCustomId").val(),
   contractTemplateId: $("#contractTemplateId").val(),
   contractHtmlCode: $("#htmlShow").html()
  }),
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success : function(data) {
   var flag = data.status ==="false" ? false : true;
   if(flag){
    var contractcReturn = JSON.parse(data.reJson);
    var contractCustomId = contractcReturn.contractCustomId;
    var shopContractSign = JSON.stringify(contractcReturn.shopContractSign);
    $("#shopContractSign").val(shopContractSign);
    console.log("shopContractSign:" + shopContractSign);
    $("#contractCustomId").val(contractCustomId);
    console.log("contractCustomId:" + contractCustomId);
    toastr.success("数据提交成功!");
   }else{
    toastr.error("数据提交失败!"); 
   }
  }
 });
具体例子:

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/JSON

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AllenLeungX

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值