简体   繁体   English

JSON.parse:使用PHP的错误转义字符

[英]JSON.parse: bad escaped character with php

I am facing problem with json parse. 我面临JSON解析问题。 I have this in php: 我在php中有这个:

 json_encode(getTeams(),JSON_HEX_APOS);

it return huge data. 它返回大量数据。

Sample data: for more clarification lets say i have this: 样本数据:为进一步澄清,我可以说:

my_encoded_data = 
     {
        "13": "Germany-1",
        "14": "Russia-1",
        "15": "Switzerland-1",
        "16": "Canada-1",
        "17": "USA-1",
        "18": "USA-2",
        "19": "Germany-2",
        "20": "Italy-1",
        "21": "Switzerland-2",
        "22": "Austria-1",
        "23": "Italy-2",
        "24": "Netherlands-1",
        "25": "Poland-1",
        "26": "Latvia-1",
        "27": "Russia-2",
        "28": "Czech Republic-1",
        "29": "Great Britain-1",
        "30": "France-1",
        "31": "Canada-2",
        "32": "Slovakia-1",
        "43": "A. Florschütz/T. Wustlich",
        "44": "P. Leitner/A. Resch",
        "46": "G. Albrecht/E. Pothier",
        "48": "C. Moffat/M. Moffat",
        "50": "V. Boizov/D. Khamkin",
        "51": "M. Kuzmitch/J. Veselov",
        "53": "T. Schiegl/M. Schiegl",
        "56": "P. Griffal/D. Joye",
        "59": "A. Linger/W. Linger",
        "62": "G. Plankensteiner/O. Haselrieder",
        "65": "A. Sics/J. Sics",
        "68": "C. Oberstolz/P. Gruber"
    }

I have tested this data by spilt it into small parts to validate in jsonlint. 我已经通过将数据分成小部分进行测试以在jsonlint中进行验证。 it shows me valid json. 它显示了有效的json。 then i have assigned it in javascript in: 那么我已经在javascript中将其分配了:

window.objteamsFromServer  = my_encoded_data;

Then i wanted to parse it in json like this: 然后我想像这样在json中解析它:

arrSearch = window.objteamsFromServer;

It gives me this error: 它给了我这个错误:

JSON.parse: bad escaped character 

how can i solve this? 我该如何解决?

let me know if any informations needed . 让我知道是否需要任何信息。

Thanks, 谢谢,
Awlad Awlad

It's very hard to tell from your question exactly what my_encoded_data is, but it sounds as though you're outputting the result of json_encode into JavaScript source code, eg (from the browser's perspective): 从您的问题中很难确切地知道my_encoded_data是什么,但是听起来好像您正在将json_encode的结果输出到JavaScript源代码中,例如(从浏览器的角度来看):

window.objteamsFromServer = {
    "13": "Germany-1",
    "14": "Russia-1",
    "15": "Switzerland-1",
    "16": "Canada-1",
    "17": "USA-1",
    // ...and so on
};

and then also trying to parse it with JSON.parse . 然后尝试使用JSON.parse对其进行解析。

You wouldn't do that. 你不会那样做。 The JavaScript engine will have already parsed that object initializer (it's not JSON, it's JavaScript source code), so you would just use the value directly: JavaScript引擎将已经解析了该对象初始化程序(它不是JSON,而是JavaScript源代码),因此您只需直接使用该值即可:

console.log(window.objteamsFromServer[13]); // "Germany-1"

I guess you might be getting this error because of the following line: 我猜您可能由于以下行而收到此错误:

"43": "A. Florschütz/T. Wustlich",

Notice the 'ü' in the name above. 注意上面名称中的“ü”。

One approach is to encode the data at server side and then decode it on client side. 一种方法是在服务器端对数据进行编码,然后在客户端进行解码。 Use JavaScripts decodeURIComponent function do decode at client side. 使用JavaScript的decodeURIComponent函数在客户端进行解码。

For server-side encoding of data, please refer php documentation. 有关数据的服务器端编码,请参阅php文档。

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

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