解析Apple JSON

Jon*_*nes 2 iphone cocoa cocoa-touch ios

通常我正在解析像(例子)的JSON提要;

{
    "AppStore": [
        {
            "Application": "Game",
            "KeyID": "zero"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

在我的JSON中,我正在做

NSArray* Parse = [json objectForKey:@"AppStore"]; 
Run Code Online (Sandbox Code Playgroud)

但我现在想解析它有另一个结构的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+"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

如果我做;

NSArray* Parse = [json objectForKey:@"resultCount"]; or 
NSArray* Parse = [json objectForKey:@"results"]; 
Run Code Online (Sandbox Code Playgroud)

该应用程序正在崩溃:

*** 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) 
Run Code Online (Sandbox Code Playgroud)

编辑:

@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
Run Code Online (Sandbox Code Playgroud)

//

- (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];
}
Run Code Online (Sandbox Code Playgroud)

omz*_*omz 6

错误消息说responseDatanil,所以它不是真正的JSON解析问题.

由于您没有展示如何构造responseData,我不知道实际问题是什么,但它在您尝试解析它之前发生.