2015-09-07 69 views
0

我的JSON检索JSON:广东话使用Ajax

{ 
"operatorname": "LUKUP", 
"Schedule": { 
"channel": [ 
    { 
    "bouquet": "Music", 
    "channelgenre": "English Music", 
    "prepaid_price": 15 
    }, 
    { 
    "bouquet": "News", 
    "channelgenre": "English News", 
    "prepaid_price": 7 
    } 
    ] 
} 
} 

Ajax调用:

$.ajax({ 
     type: "POST", 
     url: my_url, 
     async: false, 
     success: function(result){ 
      alert(JSON.stringify(result)); 
     message= JSON.parse(result); 
     alert(message.Schedule.channel.length); 
     } 
    }); 

我的JSON来了。第一条警报消息是给我的JSON。当我解析JSON,错误快到像

未捕获的SyntaxError:意外的标记Ø
地点:jquery.min.js.2

我试图解决这一问题。无法弄清楚它出错的地方。

谁能帮我

+0

这意味着存在错误的JSON格式 – Tushar

+0

我使用JSON验证 – Priya

+0

检查验证了我的JSON''中typeof运算success' result' – Tushar

回答

1

"prepaid_price": 15"prepaid_price": 7后删除昏迷。您还错过了JSON结尾处的关闭}

您可以验证您的JSON在这里:http://www.jsoneditoronline.org/

{ 
    "operatorname": "LUKUP", 
    "Schedule": { 
     "channel": [ 
      { 
       "bouquet": "Music", 
       "channelgenre": "English Music", 
       "prepaid_price": 15 
      }, 
      { 
       "bouquet": "News", 
       "channelgenre": "English News", 
       "prepaid_price": 7 
      } 
     ] 
    } 
} 
0

你可以在你的AJAX调用中使用dataType: 'json',,那么你的JSON将被自动解析,你不需要调用message = JSON.parse(result);,因为你的结果将是已经是一个对象。

然后您可以直接拨打alert(result.Schedule.channel.length);

你的代码是这样:

$.ajax({ 
    type: "POST", 
    url: my_url, 
    async: false, 
    dataType: 'json', 
    success: function(result){ 
     alert(result.Schedule.channel.length); 
    } 
});