【问题标题】:NSJSONSerializationNSJSON序列化
【发布时间】:2012-01-30 20:43:30
【问题描述】:

我遇到了一些公共 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);

【问题讨论】:

    标签: json ios5 nsjsonserialization


    【解决方案1】:

    使用最新的网络 API 我已经解决了我的问题。

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

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

    完成了

    【讨论】:

      【解决方案2】:

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

      所以你必须想办法解决这个问题。例如,您可以在响应中搜索字符串 jsonFlickrFeed( 并将其删除,或者 - 更简单的方法 - 只是切断开头,直到有效的 JSON 开始并切断最后一个字符(这应该是括号)。

      【讨论】:

        【解决方案3】:

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

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

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

        //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

        【讨论】:

          【解决方案4】:

          问题在于 Flickr 以 jsonp(带填充的 JSON)格式而不是 json 格式返回数据,因此要解决此问题,只需在 Flickr URL 中添加以下内容:

          nojsoncallback=1

          所以如果网址是:

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

          然后将其更改为:

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

          就是这样。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-10-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多