NSJSON序列化
我在使用某些公共 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用最新的 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
完成。
Using the latest web API I have solved my problem.
For reference, the old web API is:
http://api.flickr.com/services/feeds/photos_public.gne?id=xxxxxxxx&lang=en-us&format=json
The current web API is:
http://api.flickr.com/services/feeds/photos_public.gne?id=xxxxxxxx&lang=en-us&format=json&nojsoncallback=1
It's done.
我认为 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).
Flickr 使用 JSON 做了一些解析器无法处理的坏事:
对于那些希望处理 Flick JSON feed 的人来说
,故事的寓意是 - Flickr 很顽皮 - smack。
Flickr does some bad things with their JSON that the parser can't cope with:
For those looking to process the Flick JSON feed
Moral to the story - Flickr is naughty - smack.
问题是 Flickr 以 jsonp(JSON with Padding) 格式而不是 json 格式返回数据,因此要解决此问题,只需将以下内容添加到 Flickr URL:
nojsoncallback=1
因此,如果 URL 为:
然后将其更改为:
就这些。
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 :
then change it to :
That's all.