2012-05-06 22 views
2

通常、私は(EXAMPLE)のようなJSONフィードを解析しています。私のJSONでApple JSONの構文解析

{ 
    "AppStore": [ 
     { 
      "Application": "Game", 
      "KeyID": "zero" 
     } 
    ] 
} 

私はその後、

NSArray* Parse = [json objectForKey:@"AppStore"]; 

をやっているが、私は今のAppStoreのJSONを解析することは、他の構造を有している

http://itunes.apple.com/lookup?id=387633954

{ 
    "resultCount": 1, 
    "results": [ 
     { 
      "kind": "software", 
      "features": [ 
       "gameCenter", 
       "iosUniversal" 
      ], 
      "supportedDevices": [ 
       "all" 
      ], 
      "isGameCenterEnabled": true, 
      "artistViewUrl": "http://itunes.apple.com/us/artist/mcsebi/id387633957?uo=4", 
      "artworkUrl60": "http://a5.mzstatic.com/us/r1000/072/Purple/v4/98/a6/56/98a656e8-666a-89ab-c8f0-878b61b9f8f2/Icon.png", 
      "screenshotUrls": [ 
       "http://a3.mzstatic.com/us/r1000/076/Purple/v4/fe/36/80/fe3680ae-5439-a2f1-6675-d3246b64f333/mza_11015238406403423.png", 
       "http://a4.mzstatic.com/us/r1000/099/Purple/v4/73/98/1c/73981cbf-fb79-dc38-9dac-61ab46c2ac60/mzl.gzibbhfx.png", 
       "http://a3.mzstatic.com/us/r1000/103/Purple/v4/72/b6/87/72b68713-7e5a-f675-83c3-85c2f0e96df1/mzl.gykakuht.png", 
       "http://a1.mzstatic.com/us/r1000/113/Purple/v4/fa/d6/c4/fad6c453-9da5-2506-2b06-42ed5ff6bd63/mzl.hqbrlhog.png", 
       "http://a2.mzstatic.com/us/r1000/120/Purple/v4/9e/7a/13/9e7a1388-5089-11e7-0c03-3d1868abbb82/mzl.jogfcyqv.png" 
      ], 
      "ipadScreenshotUrls": [ 
       "http://a1.mzstatic.com/us/r1000/096/Purple/v4/1f/5a/92/1f5a9249-03b8-90dc-8226-18b1785d5f7d/mzl.ilqgfose.1024x1024-65.jpg", 
       "http://a5.mzstatic.com/us/r1000/070/Purple/v4/79/74/6b/79746ba8-7ea0-6e81-93d0-9e2493412e21/mzl.saleskru.1024x1024-65.jpg", 
       "http://a1.mzstatic.com/us/r1000/068/Purple/v4/12/38/91/1238914f-7387-9c9e-06e4-11a55871b04a/mzl.qmiockwv.1024x1024-65.jpg", 
       "http://a4.mzstatic.com/us/r1000/091/Purple/v4/3b/cf/62/3bcf62e6-4d84-4fc0-e721-c328361f5c95/mzl.somobtxy.1024x1024-65.jpg", 
       "http://a1.mzstatic.com/us/r1000/074/Purple/v4/e9/d3/e7/e9d3e7b7-b52c-e204-e4b6-1ab5de79e85a/mzl.nedztyfk.1024x1024-65.jpg" 
      ], 
      "artworkUrl512": "http://a3.mzstatic.com/us/r1000/115/Purple/v4/0d/9c/ec/0d9cec04-91b3-5fe2-fa40-100660250f4a/mzl.kavmnegm.png", 
      "artistId": 387633957, 
      "artistName": "McSebi", 
      "price": 0.99, 
      "version": "1.2", 
      "description": "Connect M is a simple, but very addictive puzzle game. Connect two tiles of the same color and try to remove all the tiles within a time limit. Connect M features:\n \n· 37 levels in 9 worlds. Each world has a new feature\n· High quality graphics\n· High quality sound effects and soundtrack\n\nFollow us on Twitter: http://twitter.com/qt_games\n\nMany customers demanded to save a game explicitely.\nNow you can do it by pressing the disk icon after pausing a game.", 
      "genreIds": [ 
       "6014", 
       "7003", 
       "7012" 
      ], 
      "releaseDate": "2010-08-24T02:46:16Z", 
      "sellerName": "Sebastian Wegner", 
      "currency": "USD", 
      "genres": [ 
       "Games", 
       "Arcade", 
       "Puzzle" 
      ], 
      "bundleId": "com.mcsebi.connectm", 
      "trackId": 387633954, 
      "trackName": "Connect M", 
      "primaryGenreName": "Games", 
      "primaryGenreId": 6014, 
      "releaseNotes": "Level selection:\nYou can now choose to start the game in every world you completed at least once.", 
      "wrapperType": "software", 
      "trackCensoredName": "Connect M", 
      "trackViewUrl": "http://itunes.apple.com/us/app/connect-m/id387633954?mt=8&uo=4", 
      "contentAdvisoryRating": "4+", 
      "artworkUrl100": "http://a3.mzstatic.com/us/r1000/115/Purple/v4/0d/9c/ec/0d9cec04-91b3-5fe2-fa40-100660250f4a/mzl.kavmnegm.png", 
      "languageCodesISO2A": [ 
       "DE", 
       "EN", 
       "IT", 
       "NL" 
      ], 
      "fileSizeBytes": "24434872", 
      "sellerUrl": "http://www.mcsebi.com", 
      "trackContentRating": "4+" 
     } 
    ] 
} 

私が行った場合;

NSArray* Parse = [json objectForKey:@"resultCount"]; or 
NSArray* Parse = [json objectForKey:@"results"]; 

アプリケーションがクラッシュしている:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil' 
*** First throw call stack: 
(0x352b888f 0x366df259 0x352b8789 0x352b87ab 0x311c05b3 0x8c9e3 0x352171fb 0x3116e747 0x3528cad3 0x3528c29f 0x3528b045 0x3520e4a5 0x3520e36d 0x3650a439 0x32304e7d 0x807a5 0x80240) 
terminate called throwing an exception(lldb) 

編集:

@interface NSDictionary(JSONCategories) 
+(NSDictionary*)dictionaryWithContentsOfJSONURLString:(NSString*)urlAddress; 
-(NSData*)toJSON; 
@end 

@implementation NSDictionary(JSONCategories) 
+(NSDictionary*)dictionaryWithContentsOfJSONURLString:(NSString*)urlAddress 
{ 
    NSData* data = [NSData dataWithContentsOfURL: [NSURL URLWithString: urlAddress] ]; 
    __autoreleasing NSError* error = nil; 
    id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 
    if (error != nil) return nil; 
    return result; 
} 

-(NSData*)toJSON 
{ 
    NSError* error = nil; 
    id result = [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:&error]; 
    if (error != nil) return nil; 
    return result;  
} 
@end 

//

- (void)fetchedData:(NSData *)responseData { 

    NSError* error; 
     NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData //1 
                  options:kNilOptions 
                   error:&error]; 
     NSArray* UrlParse = [json objectForKey:@"resultCount"]; 

     NSDictionary* SParse = [urlParse objectAtIndex:0]; 
} 
+0

いずれの場合でも 'json'はどのように構築されますか? –

+0

@MattBall私の編集をご覧ください。 – Jones

+0

多分、JSONKitでやってみてください。それはほぼ確実に速くなるでしょうが、私は、これらの種類の問題が発生したときにそれがより信頼できることを発見しました。 – farski

答えて

6

エラーメッセージがresponseDatanilであると言うので、それは本当にありませんJSON解析の問題

responseDataをどのように構築するかを示していないので、実際の問題は何か分かりませんが、解析する前に発生します。

+1

私は私のメッセージを編集しました。あなたが今私を助けてくれることを願っています。 – Jones

+0

@Jones - デバッガの使用を検討しましたか? –

0
NSData* data = [NSData dataWithContentsOfURL: [NSURL URLWithString: urlAddress] ]; 
__autoreleasing NSError* error = nil; 
id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

2つのこと:

  1. urlAddressが本当にURLではなく、パスを記述していることを確認してください。パスの場合は、fileURLWithPath:を使用してNSURLオブジェクトを作成します。 (App Storeから情報を取得しようとしているので、この問題はないかもしれませんが、ローカルファイルからJSONを読み込もうとしている将来の読者は、間違いを犯している可能性があります)。
  2. URLまたはパス(いずれかのもの)は正しいものであり、存在するものを記述します。存在しないリソースの内容を取得しようとすると、datanilの理由が説明されます。

また、デバッガを使用して、2つのJSONからオブジェクトへのメソッドが例外を生成しているかどうかを確認する必要があります。これらのいずれかがそれかもしれません。上記はあなたのためのものです+[NSDictionary(JSONCategories) dictionaryWithContentsOfJSONURLString:];例外がfetchedData:から来ている場合、fetchedData:nilと呼んでいることを意味し、質問を編集してfetchedData:というコードを表示し、そのコードがどのようにデータを取得(または取得しようとする)するかを示す必要があります。