NSJSON序列化

发布于 2024-12-23 13:28:40 字数 729 浏览 1 评论 0原文

我在使用某些公共 json 服务时遇到问题 以这种方式格式化的服务

jsonFlickrFeed({
        "title": "Uploads from everyone",
        "link": "http://www.flickr.com/photos/",
        "description": "",
        "modifi ... })

NSJSONSerialization 似乎无法使其工作

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);

I have problems with some public json services
with serveices formatted this way

jsonFlickrFeed({
        "title": "Uploads from everyone",
        "link": "http://www.flickr.com/photos/",
        "description": "",
        "modifi ... })

NSJSONSerialization seems to be unable to make its work

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鹿港巷口少年归 2024-12-30 13:28:40

我认为 NSJSONSerialization 无法处理 JSON 代码中的 jsonFlickrFeed ( ... ) 。问题是纯响应是无效的 JSON(在此处进行测试)。

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

I think that NSJSONSerialization can't deal with the jsonFlickrFeed ( ... ) in your JSON code. The problem is that the pure response is invalid JSON (test it here).

So you will have to build a way around this issue. You could for example search for the string jsonFlickrFeed( in the response and delete it or - the easier way - just cut off the beginning until the valid JSON starts and cut off the last character (which should be the bracket).

荭秂 2024-12-30 13:28:40

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

  • 它们以 'jsonFlickrFeed(' 开头,以 ')' 结尾
    • 这意味着根对象无效
  • 它们错误地转义了单引号.. 例如。 \'
    • 这是无效的 JSON,会让解析器感到难过!

对于那些希望处理 Flick JSON feed 的人来说

//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 很顽皮 - smack

Flickr does some bad things with their JSON that the parser can't cope with:

  • they start with 'jsonFlickrFeed(' and end with ')'
    • this means that the root object is not valid
  • they incorrectly escape single quotes .. eg. \'
    • this is invalid JSON and will make the parser sad!

For those looking to process the Flick JSON feed

//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);
}

Moral to the story - Flickr is naughty - smack.

逐鹿 2024-12-30 13:28:40

问题是 Flickr 以 jsonp(JSON with Padding) 格式而不是 json 格式返回数据,因此要解决此问题,只需将以下内容添加到 Flickr URL:

nojsoncallback=1

因此,如果 URL 为:

https://www.flickr.com/services/feeds/photos_public.gne?format=json

然后将其更改为:

https://www.flickr.com/services/feeds/photos_public.gne?format=json&nojsoncallback=1

就这些。

The problem is that Flickr is returning data in jsonp(JSON with Padding) format instead of json format so to solve this issue just add following to the Flickr URL:

nojsoncallback=1

So if the URL is :

https://www.flickr.com/services/feeds/photos_public.gne?format=json

then change it to :

https://www.flickr.com/services/feeds/photos_public.gne?format=json&nojsoncallback=1

That's all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文