JS
js对象转json字符串
params = {}
params.attachmentIds = uploadAttachmentIds.join(",");
writeCookie("Imges", JSON.stringify(params), 1);
json字符串转js对象
var imgs = JSON.parse(readCookie("Imges"));
Java 传递json字符串并解析
前台
var dataArr = new Array();
var Row = _self.getSelectRows();
for(var i=0;i<Row.length;i++)
{
var iceData = new Object();
iceData.adviceFlowId = Row[i].iceFlowId;
iceData.companyId = Row[i].comId;
dataArr.push(iceData);
}
var checkUrl = CONTEXT_PATH +"/ice/beforeDel"
var param ={
"JsonArray":$.toJSON(dataArr)
}
var retData = $.getData(checkUrl,param);
后台接收json字符串
import com.alibaba.fastjson.JSON;
@RequestMapping("/ice/beforeDel")
@ResponseBody
public AssembleJSON beforedeleteAdvice(String JsonArray,HttpServletRequest request)
{
String localOrgId = user.getOrgId();
HashMap<String,Object> hashMap = new HashMap<String,Object>();
List<String> ids = new ArrayList<>();
String companyId = "";
List<ice> iceList = JSON.parseArray(JsonArray, ice.class);
for(ice ice:iceList) {
String iceFlowId =ice.getIceFlowId();
comId = ice.getComId();
AdviceFLowNodeVO upFLowNodeVO = new AdviceFLowNodeVO();
upFLowNodeVO.setAdviceFlowId(iceFlowId);
upFLowNodeVO.setCompanyId(comId);
AdviceFLowNodeVO adviceFLowNodeVO = new AdviceFLowNodeVO();
adviceFLowNodeVO = adviceFlowNodeService.getUpAdvice(upFLowNodeVO);
}
Java的Json字符串转string
public static String transJsonToString(String json)
{
String s = json.replaceAll("\\{\"","{'") // {" --> {'
.replaceAll("\":\"", "':'") // ":" --> ':'
.replaceAll("\",\"", "','") // "," --> ','
.replaceAll("\":", "':") // ": --> ':
.replaceAll(",\"", ",'") // ," --> ,'
.replaceAll("\"\\}", "'}") // "} --> '}
.replaceAll("\"", "") // " --> 空,以去掉字符串中间的引号
.replaceAll("'", "\"") // ' --> ",所有的单引号替换回双引号
.replaceAll("<br />", "") // 去掉字符串中的<br />
.replaceAll("\\\\", "?"); // 字符串中含\也会导致解析失败
return s;
}