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

你可能感兴趣的:(json)