最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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
发布评论

评论列表(0)

  1. 暂无评论