开发者

special char in JSON

开发者 https://www.devze.com 2023-04-01 19:44 出处:网络
I should send Hi\" to a Yahoo server, so in PHP I should place \\ befor the \", but it will get bad JSON ar开发者_运维知识库guments. How should I do it?

I should send Hi" to a Yahoo server, so in PHP I should place \ befor the ", but it will get bad JSON ar开发者_运维知识库guments. How should I do it?

Place Hi" in JSON code without error?

$message = "Hi\"";
$postdata = '{
             "message" : "'.$message.'"
             }';


Use json_encode instead of hand-crafting JSON:

$postdata = json_encode(array("message" => $message));

If you must handcraft your JSON, don't forget to add a backslash before a quotation mark:

$message = "Hi\\\"";
// or, more clearly ...
$message = 'Hi\\"';


New line character will not work in case of Tooltip with some browsers.
Not working \r\n or \n
Not working single quotes \'abcd

Use double backslash to escape characters.

Solution : use '\\\r\\\n' in place of '\r\n' ,
it will solve your problem.
Happy coding...!

0

精彩评论

暂无评论...
验证码 换一张
取 消