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 k8s生产配置推荐配置及部署方案
  • ¥15 matlab提取运动物体的坐标
  • ¥15 人大金仓下载,有人知道怎么解决吗
  • ¥15 一个小问题,本人刚入门,哪位可以help
  • ¥15 python安卓开发
  • ¥15 使用R语言GD包一直不出结果
  • ¥15 计算机微处理器与接口技术相关问题,求解答图片的这个问题,有多少个端口,端口地址和解答问题的方法和思路,不要AI作答
  • ¥15 如何根据一个截图编写对应的HTML代码
  • ¥15 stm32标准库的PID角度环
  • ¥15 ADS已经下载好了,但是DAS下载不了,一直显示这两种情况,有什么办法吗,非常急!