javascript - Parsing a data feed results in a Invalid Character error in WinJS - Stack Overflow

I am calling the Flickr data feed in WinJS for a Windows 8 Metro App. When I attempt to parse the feed

I am calling the Flickr data feed in WinJS for a Windows 8 Metro App. When I attempt to parse the feed response with JSON.parse, I get an Invalid Character error. Here is my code:

function processPhotos(result)
{
    var photoData = JSON.parse(result.responseText);
    //bind here
    data.items.forEach(function (item) {
        list.push(item);
    });
}

function processError(error) {
    console.log(error.message);
}

WinJS.xhr({ url: ".gne?format=json" }).then(processPhotos, processError);
WinJS.Namespace.define("data", {
    items: groupedItems,
    groups: groupedItems.groups,
    getItemsFromGroup: getItemsFromGroup
});

Result.ResponseText has the expected content.

Does anyone else encounter this?

I am calling the Flickr data feed in WinJS for a Windows 8 Metro App. When I attempt to parse the feed response with JSON.parse, I get an Invalid Character error. Here is my code:

function processPhotos(result)
{
    var photoData = JSON.parse(result.responseText);
    //bind here
    data.items.forEach(function (item) {
        list.push(item);
    });
}

function processError(error) {
    console.log(error.message);
}

WinJS.xhr({ url: "http://api.flickr./services/feeds/photos_public.gne?format=json" }).then(processPhotos, processError);
WinJS.Namespace.define("data", {
    items: groupedItems,
    groups: groupedItems.groups,
    getItemsFromGroup: getItemsFromGroup
});

Result.ResponseText has the expected content.

Does anyone else encounter this?

Share Improve this question asked Mar 13, 2012 at 4:27 Bill SempfBill Sempf 9762 gold badges14 silver badges40 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

I had to do this to clear out some of the invalid characters in the responseText. (suggested to me by https://stackoverflow./users/200698/devhammer)

var cleansed = result.responseText.replace(/\\'/g, "'");
var photoData = JSON.parse(cleansed).d;

If you look at the data, you will notice it's not JSON, it's JSONP. That's the reason why JSON.parse() can't process it. If you want normal JSON, according to the documentation, you should use nojsoncallback=1:

http://api.flickr./services/feeds/photos_public.gne?format=json&nojsoncallback=1

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745371294a4624806.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信