JSON.parse:为什么这不起作用

时间:2016-03-04 16:22:09

标签: javascript jquery

我有一个JSON:

$.ajax({
        type: "POST",
        url: myUrl,
        success: function (result) {
             var data = JSON.parse(result);
             for (var i = 0; i < data.poles.length; i++) {
             ....

我在调试器中看到的是:

data = "{"poles":[{"id":36,"name":"AUVERGNE"},{"id":44,"name":"Alsace"},{"id":42,"name":"Artois"},....],"poleNumber":48}

但是我收到一条消息,说没有定义data.poles

我错过了什么?

1 个答案:

答案 0 :(得分:1)

Json好吗?

{"poles":[{"id":36,"name":"AUVERGNE"},{"id":44,"name":"Alsace"},{"id":42,"name":"Artois"}],"poleNumber":48}

编辑:

好的,如果你有这个JSON:

data = {"poles":[{"id":36,"name":"AUVERGNE"},{"id":44,"name":"Alsace"},{"id":42,"name":"Artois"}],"poleNumber":48}

你可以完美地制作data.poles。删除第一个&#34;,你有一个错误的json,所以你永远不会解析

"{"poles":[{"id":36,"name":"AUVERGNE"},{"id":44,"name":"Alsace"},{"id":42,"name":"Artois"}],"poleNumber":48}