javascript Javascript解码包含编码字符串的JSON字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17178708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:23:39  来源:igfitidea点击:

Javascript decode JSON string which contains an encoded string

phpjavascriptjqueryjsonescaping

提问by periklis

I have the following PHP code:

我有以下 PHP 代码:

    $foo = new stdClass();
    $foo->test='hello world';
    $bar = new stdClass();
    $bar->foo = json_encode($foo);
    $encoded_string = json_encode($bar);

The $encoded_stringcontains:

其中$encoded_string包含:

{"foo":"{\"test\":\"hello world\"}"}

I want to parse this string from javascript (using jQuery's $.parseJSONfor example):

我想从 javascript 解析这个字符串($.parseJSON例如使用 jQuery ):

var data = $.parseJSON('{"foo":"{\"test\":\"hello world\"}"}');
console.log(data);

I would expect something like the following to be logged:

我希望记录如下内容:

Object {foo: '{"test":"hello world"}'}

But I get an Unexpected token terror when running it (using chromium)

但是我Unexpected token t在运行它时出错(使用铬)

How can I parse this json string in Javascript? Here's a fiddleif anyone wants to try.

如何在 Javascript 中解析这个 json 字符串?如果有人想尝试,这里有一个小提琴

回答by zzzzBov

The problem that you're running into is that the output of json_encodeis notmeant to be used directly as a string in JavaScript.

你正在运行到的问题是,输出json_encode不是意味着被直接用作在JavaScript字符串。

json_encodeoutputs a usable JavaScript object:

json_encode输出一个可用的 JavaScript 对象:

<?php
$foo = new stdClass();
$foo->test='hello world';
$bar = new stdClass();
$bar->foo = json_encode($foo);
$encoded_string = json_encode($bar);
?>
var a = <?php $encoded_string ?>;
console.log(a.foo); // produces '{"test":"hello world"}'

If you wantto needlessly parse the JSON output from a string value, you simply need to double encode $encoded_string:

如果您从字符串值不必要地解析 JSON 输出,您只需要双重编码$encoded_string

<?php
$foo = new stdClass();
$foo->test='hello world';
$bar = new stdClass();
$bar->foo = json_encode($foo);
$encoded_string = json_encode(json_encode($bar));
?>
var aStr = <?php $encoded_string ?>;
var a = JSON.parse(aStr);
console.log(a.foo); //same as before


Of course, you should avoid using server side languages to generate JavaScript code, instead set up the data as either a data-*attributeor as a JSON source that can be requested with AJAX.

当然,您应该避免使用服务器端语言来生成 JavaScript 代码,而是将数据设置为可以使用 AJAX 请求的data-*属性或 JSON 源。

When the data is requested from the server (or from the attribute) it will be as a properly escaped JavaScript string, which is where JSON.parsewill be necessary to parse the object.

当从服务器(或从属性)请求数据时,它将作为正确转义的 JavaScript 字符串,这是JSON.parse解析对象所必需的。

回答by Esailija

Your code should be

你的代码应该是

$foo = new stdClass();
$foo->test='hello world';
$bar = new stdClass();
$bar->foo = $foo;
$encoded_string = json_encode($bar);

Just json encode once at the end, and decode once at the beginning at the other end.

只需json在最后编码一次,在另一端开始解码一次。



As for the jsfiddle, you are not considering that the string literals go through additional decoding layer before they become "strings in javascript memory".

至于 jsfiddle,您没有考虑到字符串文字在成为“javascript 内存中的字符串”之前要经过额外的解码层。

The correct way to setup the string literal in this case is (JS-JSON-JSON):

在这种情况下设置字符串文字的正确方法是 (JS-JSON-JSON):

data = $.parseJSON('{"foo":"{\\"test\\":\\"hello world\\"}"}');
console.log($.parseJSON(data.foo));

And simply reversing the encoding steps you did works. http://jsfiddle.net/Jmjjp/2/

只需颠倒您所做的编码步骤即可。http://jsfiddle.net/Jmjjp/2/

回答by scum

The problem is double encoding as JSON. The solution you want if you need to retain the data as a string is

问题是双重编码为​​ JSON。如果您需要将数据保留为字符串,您想要的解决方案是

$bar->foo = addslashes(json_encode($foo));

$bar->foo = addslashes(json_encode($foo));

回答by Pinoniq

The code returns exactly what it should return.

代码返回的正是它应该返回的内容。

when json_encodein $bar, $bar->foo is a string. this string is escaped to produce a correct output.

当 json_encodein $bar 时,$bar->foo 是一个字符串。该字符串被转义以产生正确的输出。

$bar->foo = json_encode($foo);

should be $bar->foo = $foo

应该 $bar->foo = $foo

回答by dandavis

you need to escape the slashes protecting the inner quotes:

您需要转义保护内部引号的斜杠:

JSON.parse('{"foo":"{\"test\":\"hello world\"}"}');
// == Object {foo: '{"test":"hello world"}'}