8

このようにフォーマットされたサービスを持ついくつかの公開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);
4

4 に答える 4

17

最新の Web API を使用して、問題を解決しました。

参考までに、古い Web APIhttp://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

完了です。

于 2013-09-25T06:58:33.320 に答える
5

NSJSONSerializationは、JSON コードのjsonFlickrFeed ( ... )を処理できないと思います。問題は、純粋な応答が無効な JSON であることです (ここでテストしてください)。

したがって、この問題を回避する方法を構築する必要があります。たとえば、応答で文字列jsonFlickrFeed(を検索して削除するか、より簡単な方法で、有効な JSON が開始するまで先頭を切り取り、最後の文字 (括弧である必要があります) を切り捨てることができます。

于 2011-12-31T10:56:06.403 に答える
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 はいたずらだ -ピシャリ

于 2013-04-24T08:41:51.293 に答える
0

問題は、Flickr が json 形式ではなく jsonp (パディング付きの 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

それで全部です。

于 2020-09-16T08:26:02.353 に答える