简体   繁体   English

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

[英]Javascript decode JSON string which contains an encoded string

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_string contains: $encoded_string包含:

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

I want to parse this string from javascript (using jQuery's $.parseJSON for example): 我想从javascript解析这个字符串(例如使用jQuery的$.parseJSON ):

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 t error when running it (using chromium) 但是在运行它时会出现Unexpected token t错误(使用铬)

How can I parse this json string in Javascript? 如何在Javascript中解析这个json字符串? Here's a fiddle if anyone wants to try. 如果有人想尝试, 这是一个小提琴

The problem that you're running into is that the output of json_encode is not meant to be used directly as a string in JavaScript. 您遇到的问题是json_encode的输出不能直接用作JavaScript中的字符串。

json_encode outputs 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 want to 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-* attribute or as a JSON source that can be requested with AJAX. 当然,您应该避免使用服务器端语言来生成JavaScript代码,而是将数据设置为data-*属性或可以使用AJAX请求的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.parse will be necessary to parse the object. 当从服务器(或从属性)请求数据时,它将作为正确转义的JavaScript字符串,这是解析对象所需的JSON.parse

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/

The problem is double encoding as JSON. 问题是双重编码为​​JSON。 The solution you want if you need to retain the data as a string is 如果需要将数据保留为字符串,则需要所需的解决方案

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

The code returns exactly what it should return. 代码返回它应该返回的内容。

when json_encodein $bar, $bar->foo is a string. 当json_encodein $ bar时,$ bar-> foo是一个字符串。 this string is escaped to produce a correct output. 转义此字符串以生成正确的输出。

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

should be $bar->foo = $foo 应该是$bar->foo = $foo

you need to escape the slashes protecting the inner quotes: 你需要逃避保护内部引号的斜线:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM