Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

javascript - How can parse JSon with special characters?

This my text in database:

"推奨切削条件 "

in PHP, i using Json_encode: Result:

{"table1":[{"Item":{"original_text":"u63a8u5968u5207u524au6761u4ef6 "}}]};

In javascript :

var strData ='{"table1":[{"Item":{"original_text":"u63a8u5968u5207u524au6761u4ef6 "}}]}';//getData();
strData=strData.replace(/
/g, "\n").replace(/
/g, "\r").replace(/	/g, "\t");      
var jsonData = JSON.parse(strData)

Error:

Uncaught SyntaxError: Unexpected token  in JSON at position 56
    at JSON.parse (<anonymous>)

enter image description here How can parse JSon with special characters?

Thanks all.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

i have to copy json to js

I have no idea why, but okay… You don't need to deal with it as JSON string inside Javascript at all then. JSON is a valid Javascript literal. So just paste your JSON into your Javascript without the quotes:

var data = {"table1":[{"Item":{"original_text":"u63a8u5968u5207u524au6761u4ef6 "}}]};

This is a perfectly valid Javascript object literal which requires no parsing.

If you need a valid Javascript string literal containing a JSON string, json_encode(json_encode(...)) it twice to get:

var strData = "{"table1":"\u63a8\u5968\u5207\u524a\u6761\u4ef6 \b"}";
console.log(JSON.parse(strData));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...