首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSJSONSerialization

NSJSONSerialization
EN

Stack Overflow用户
提问于 2011-12-31 06:26:50
回答 4查看 6.2K关注 0票数 8

我对一些带有这种格式的服务的公共json服务有问题。

代码语言:javascript
复制
jsonFlickrFeed({
        "title": "Uploads from everyone",
        "link": "http://www.flickr.com/photos/",
        "description": "",
        "modifi ... })

NSJSONSerialization似乎无法让它的工作

代码语言:javascript
复制
NSURL *jsonUrl = [NSURL URLWithString:@"http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=yahoo&callback=YAHOO.Finance.SymbolSuggest.ssCallback"];
NSError *error = nil;
NSData *jsonData = [NSData dataWithContentsOfURL:jsonUrl options:kNilOptions error:&error];
NSMutableDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
NSLog(@"%@", jsonResponse);
EN

回答 4

Stack Overflow用户

发布于 2013-09-25 14:58:33

使用最新的web API,我已经解决了我的问题。

作为参考,旧的web API为:http://api.flickr.com/services/feeds/photos_public.gne?id=xxxxxxxx&lang=en-us&format=json

当前的web API为:http://api.flickr.com/services/feeds/photos_public.gne?id=xxxxxxxx&lang=en-us&format=json&nojsoncallback=1

做完了,。

票数 17
EN

Stack Overflow用户

发布于 2011-12-31 18:56:06

我认为NSJSONSerialization不能处理jsonFlickrFeed ( ... )在您的JSON代码中。问题是纯响应是无效的JSON (测试它here)。

因此,您必须构建一种解决此问题的方法。例如,您可以搜索字符串jsonFlickrFeed(在响应中将其删除),或者-更简单的方法-只是在有效的JSON开始之前截断开头,并去掉最后一个字符(应该是括号)。

票数 5
EN

Stack Overflow用户

发布于 2013-04-24 16:41:51

Flickr使用JSON做了一些解析器无法处理的坏事:

  • 以'jsonFlickrFeed(‘’开头,以')‘结尾
    • 这意味着根对象不是valid

  • 他们错误地转义单引号..例如:\‘
    • 这是无效的JSON,将使解析器成为JSON

对于那些希望处理Flick JSON提要的人

代码语言:javascript
复制
//get the feed
NSURL *flickrFeedURL = [NSURL URLWithString:@"http://api.flickr.com/services/feeds/photos_public.gne?format=json&tags=data"];
NSData *badJSON = [NSData dataWithContentsOfURL:flickrFeedURL];
//convert to UTF8 encoded string so that we can manipulate the 'badness' out of Flickr's feed
NSString *dataAsString = [NSString stringWithUTF8String:[badJSON bytes]];
//remove the leading 'jsonFlickrFeed(' and trailing ')' from the response data so we are left with a dictionary root object
NSString *correctedJSONString = [NSString stringWithString:[dataAsString substringWithRange:NSMakeRange (15, dataAsString.length-15-1)]];
//Flickr incorrectly tries to escape single quotes - this is invalid JSON (see http://stackoverflow.com/a/2275428/423565)
//correct by removing escape slash (note NSString also uses \ as escape character - thus we need to use \\)
correctedJSONString = [correctedJSONString stringByReplacingOccurrencesOfString:@"\\'" withString:@"'"];
//re-encode the now correct string representation of JSON back to a NSData object which can be parsed by NSJSONSerialization
NSData *correctedData = [correctedJSONString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:correctedData options:NSJSONReadingAllowFragments error:&error];
if (error) {
    NSLog(@"this still sucks - and we failed");
} else {
    NSLog(@"we successfully parsed the flickr 'JSON' feed: %@", json);
}

故事的寓意- Flickr是淘气的-扇。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8684667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档