admin管理员组文章数量:1403480
var mystring = '{"Customers":[{"CustomerCity":"Zaragoza","CustomerFName":"Ana","CustomerAddress":"C/ El Temple, 9 2\\xbaB","CustomerCountry":"es"}]}';
var myparsestring = JSON.parse(mystring);
var mystring = '{"Customers":[{"CustomerCity":"Zaragoza","CustomerFName":"Ana","CustomerAddress":"C/ El Temple, 9 2\\xbaB","CustomerCountry":"es"}]}';
var myparsestring = JSON.parse(mystring);
Error:
Share edited Aug 16, 2017 at 8:40 Rory McCrossan 338k41 gold badges320 silver badges351 bronze badges asked Aug 16, 2017 at 8:38 SarangSarang 7843 gold badges9 silver badges25 bronze badges 3Unexpected token x in JSON
- Your JSON isn't encoded properly – Rory McCrossan Commented Aug 16, 2017 at 8:42
- var mystring = '{"Customers":[{"CustomerCity":"Zaragoza","CustomerFName":"Ana","CustomerAddress":"C/ El Temple, 9 2/\/\xbaB","CustomerCountry":"es"}]}'; var myparsestring = JSON.parse(mystring); – amit wadhwani Commented Aug 16, 2017 at 8:58
- I have escaped special characters to get it parsed. – amit wadhwani Commented Aug 16, 2017 at 8:59
1 Answer
Reset to default 9That's simply invalid JSON, see the rules for strings on json. There is no \x
escape in JSON. The \xbaB
should be a unicode escape, \u0baB
(note that there must be exactly four hex digits):
var mystring ='{"Customers":[{"CustomerCity":"Zaragoza","CustomerFName":"Ana","CustomerAddress":"C/ El Temple, 9 2\\u0baB","CustomerCountry":"es"}]}';
var obj = JSON.parse(mystring);
console.log(obj);
You could try to pre-process the string:
mystring = mystring.replace(/\\x([0-9a-f]{1,4})/gi, function(m, c0) {
return "\\u" + ("0000" + c0).slice(-4);
});
var mystring ='{"Customers":[{"CustomerCity":"Zaragoza","CustomerFName":"Ana","CustomerAddress":"C/ El Temple, 9 2\\xbaB","CustomerCountry":"es"}]}';
// Fixing it
mystring = mystring.replace(/\\x([0-9a-f]{1,4})/gi, function(m, c0) {
return "\\u" + ("0000" + c0).slice(-4);
});
var obj = JSON.parse(mystring);
console.log(obj);
...but really, it would be much better to fix the source of the JSON so it produces valid JSON, and the above is a very naïve fix.
本文标签: javascriptUnable to parse JSON string containing unicode hex characterStack Overflow
版权声明:本文标题:javascript - Unable to parse JSON string containing unicode hex character - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744649926a2617623.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论