53

エスケープされていても二重引用符は解析エラーをスローしています。
以下のコードを見てください

//parse the json in javascript  
var testJson = '{"result": ["lunch", "\"Show\""] }';  
var tags = JSON.parse(testJson);  
alert (tags.result[1]);

二重引用符 (既にエスケープされている) が原因で、解析エラーがスローされます。
ここでもeval()機能しません。
しかし、次のように二重スラッシュでエスケープすると:

var result = '{"result": ["lunch", "\\"Show\\""] }';  
var tags = JSON.parse(result);  
alert (tags.result[1]);

その後、正常に動作します。
ここでJavaScriptでダブルスラッシュを使用する必要があるのはなぜですか? 問題は、PHPjson_encode()関数が単一のスラッシュ (このように: \"show\") で二重引用符をエスケープし、JSON.parse解析できないことです。この状況をどのように処理しますか?

4

9 に答える 9

35

Javascript はその文字列をエスケープ解除し、json もそれらをエスケープ解除します。最初の文字列 ( '{"result": ["lunch", "\"Show\""] }') は、json パーサーによって として認識 されます。これは、javascript では が を意味する{"result": ["lunch", ""Show""] }ためですが、二重引用符で囲まれた文字列を終了しません。\""

2 番目の文字列'{"result": ["lunch", "\\\"Show\\\""] }'は最初にエスケープ解除されます{"result": ["lunch", "\"Show\""] }(これは json によって正しくエスケープ解除されます)。

'{"result": ["lunch", "\\"Show\\""] }'それもうまくいくはずだと思います。

于 2009-06-04T10:11:46.993 に答える
33

最後に、JSON の解析は同じ eval を使用するため、それらを簡単に指定しても違いはありません。構文が正しくありません。この場合、php で引用符を正しくエスケープしてから、それらとそのエスケープ スラッシュを json_encode でエスケープする必要があります。

<?php
    $json = '{"result": ["lunch", "\"Show\""] }';
    echo json_encode($json);
?>

OUTPUT: "{\"result\": [\"lunch\", \"\\\"Show\\\"\"] }"

これは、クライアント側の JS で動作するはずです (タイプミスがなければ)。

于 2009-06-04T10:20:30.253 に答える
11

This problem is caused by the two-folded string escaping mechanism: one comes from JS and one comes from JSON.

A combination of the backslash character combined with another following character is used to represent one character that is not otherwise representable within the string. ''\\'' stands for '\' etc.

This escaping mechanism takes place before JSON.parse() works.

For Example,

var parsedObj = JSON.parse('{"sentence": "It is one backslash(\\\\)"}');
console.log(parsedObj.sentence);
>>>"It is one backslash(\)"

From the string generator's perspective, it passes four backlashes '\' into the JavaScript interpretor.

From the JavaScript interpretor's perspective, it inteprets there are two backlashes(\) as each '\\' sequence will be interpreted as one '\'.

From the JSON parser's perspective, it receives two backlashes(\\) and the JSON string escape rules will parses it as one single '\' which is the output result.

Explain you first code:

var testJson = '{"result": ["lunch", "\"Show\""] }';
//The real string after sequence escaping in to JS is
//'{"result": ["lunch", ""Show""] }' 
//which is passed into the JSON.parse.
//Thus, it breaks the JSON grammar and generates an error
var tags = JSON.parse(testJson);  
alert (tags.result[1]);
于 2011-10-26T15:09:56.693 に答える
10

ドキュメントから

JSON_HEX_APOS (整数) すべての ' は \u0027 に変換されます
JSON_HEX_QUOT(整数) すべての " は \u0022 に変換されます

json_encode() は、値とオプションの 2 つの引数を取ります。だから試してみてください

json_encode($result, JSON_HEX_QUOT); // or
json_encode($result, JSON_HEX_QUOT | JSON_HEX_APOS);

私はこれを試していません。

于 2009-06-04T10:14:19.407 に答える
3

php.iniでオフmagic_quotes_gpcにします。

于 2010-04-22T08:53:45.167 に答える