云服务器 99 / 年,新老同享(可以99/年续费),开发者力荐特惠渠道,新客户在享受9折
阿里云推广

js的字符串在解析成json数组或者对象的时候报错

  • 内容
  • 评论
  • 相关

手动拼接的字符串在转化成json数组或者json对象的时候报错,比如地址详情里面出现了回车等特殊符号。
//var msg = '[{"Action":"AddCardOrder","CardNum":"' + CarNum + '","CardPwd":"' + CardPwd + '","GoodKeyNum":"' + KeyNum + '","LinkMan":"' + LinkMan + '","CellPhone":"' + CellPhone + '","LinkTel":"","Link":"男","Province":"' + provincenew + '","City":"' + citynew + '","Area":"' + areanew + '","LinkAddress":"' + LinkAddress + '","ClientRemark":"","BigClientKeyNum":"","Period":"","VerCode":"","AppDay":"' + AppDay + '","ItemList":"'+ItemList+'"}]';


解决办法定义json对象,然后push到json数组里面去,然后在转化成字符串。
        var send_json={};
        var  send_arr=[];
        send_json["Action"]="AddCardOrder";
        send_json["CardNum"]=CarNum;
        send_json["CardPwd"]=CardPwd;
        send_json["GoodKeyNum"]=KeyNum;
        send_json["LinkMan"]=LinkMan;
        send_json["CellPhone"]=CellPhone;
        send_json["LinkTel"]="";
        send_json["Link"]="男";
        send_json["Province"]=provincenew;
        send_json["City"]=citynew;
        send_json["Area"]=areanew;
        send_json["LinkAddress"]=LinkAddress;
        send_json["AppDay"]=AppDay;
        send_json["ItemList"]=ItemList;
        send_arr.push(send_json)
       var msg=JSON.stringify(send_arr);


这样的字符串才能被解析成json数组或者对象。
   var msg_obj = JSON.parse(msg);


本文标签:

版权声明:若无特殊注明,本文皆为《菜鸟站长》原创,转载请保留文章出处。

本文链接:js的字符串在解析成json数组或者对象的时候报错 - https://wziyi.com.cn/?post=368

发表评论

电子邮件地址不会被公开。 必填项已用*标注