解析带有转义引号的JSON时出错 [英] Error Parsing JSON with escaped quotes

查看:166
本文介绍了解析带有转义引号的JSON时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从浏览器中调用URL时,我得到了以下json对象,我希望其中没有数据.

I am getting the following json object when I call the URL from Browser which I expect no data in it.

"{\"data\":[], \"SkipToken\":\"\", \"top\":\"\"}"

但是,当我尝试用javascript调用它时,它会给我error Parsing Json message

However, when I tried to call it in javascript it gives me error Parsing Json message

dspservice.callService(URL, "GET", "", function (data) {
    var dataList = JSON.parse(data);
)};

在我不知道为什么突然停止工作并抛出错误之前,这段代码已经起作用了.

This code was working before I have no idea why all of a sudden stopped working and throwing me error.

推荐答案

由于您提供给我们的JSON字符串没有任何问题,所以唯一的其他解释是data传递给您的函数是 .

Since there is nothing wrong with the JSON string you gave us, the only other explanation is that the data being passed to your function is something other than what you listed.

要检验此假设,请运行以下代码:

To test this hypothesis, run the following code:

dspservice.callService(URL, "GET", "", handler(data));

function handler(data) {
    var goodData = "{\"data\":[], \"SkipToken\":\"\", \"top\":\"\"}";
    alert(goodData);                         // display the correct JSON string
    var goodDataList = JSON.parse(goodData); // parse good string (should work)
    alert(data);                             // display string in question
    var dataList = JSON.parse(data);         // try to parse it (should fail)
}

如果可以正确解析goodData JSON字符串,并且data的格式似乎不正确,那么您可以找到问题的答案.

If the goodData JSON string can be parsed with no issues, and data appears to be incorrectly-formatted, then you have the answer to your question.

在定义了goodDatahandler函数的第一行上放置一个断点.然后逐步执行代码.从您在评论中告诉我的内容来看,它在JSON解析期间仍然崩溃,但是我敢打赌它在 second 解析而不是第一次解析中失败.

Place a breakpoint on the first line of the handler function, where goodData is defined. Then step through the code. From what you told me in your comments, it is still crashing during a JSON parse, but I'm willing to wager that it is failing on the second parse and not the first.

这篇关于解析带有转义引号的JSON时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆