douju2474 2014-01-28 11:47
浏览 107
已采纳

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 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"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题
  • ¥15 关于Python的会计设计
  • ¥15 聚类分析 设计k-均值算法分类器,对一组二维模式向量进行分类。
  • ¥15 stm32c8t6工程,使用hal库
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低