douju2474
2014-01-28 11:47 阅读 49

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

I am facing problem with json parse. I have this in 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. it shows me valid json. then i have assigned it in javascript in:

window.objteamsFromServer  = my_encoded_data;

Then i wanted to parse it in json like this:

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    doolo00026 doolo00026 2014-01-28 11:53

    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, e.g. (from the browser's perspective):

    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.

    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:

    console.log(window.objteamsFromServer[13]); // "Germany-1"
    
    点赞 评论 复制链接分享
  • doulao1934 doulao1934 2014-01-28 11:54

    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.

    For server-side encoding of data, please refer php documentation.

    点赞 评论 复制链接分享

相关推荐