2

通常我正在解析 JSON 提要,例如 (EXAMPLE) ;

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

在我的 JSON 中,我正在做

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];
}
4

2 回答 2

6

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

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

于 2012-05-06T17:32:39.083 回答
0
NSData* data = [NSData dataWithContentsOfURL: [NSURL URLWithString: urlAddress] ];
__autoreleasing NSError* error = nil;
id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

两件事情:

  1. 确保它urlAddress真实地描述了 URL 而不是路径。如果是路径,则用于fileURLWithPath:创建 NSURL 对象。(您可能没有这个问题,因为您正在尝试从 App Store 获取信息,但未来尝试从本地文件读取 JSON 的读者可能会犯此错误。)
  2. 确保 URL 或路径(无论是哪个)正确并描述了存在的事物。试图获取不存在的资源的内容可以解释为什么datanil.

此外,您应该使用调试器来验证您的两个 JSON 到对象方法中的哪一个正在产生异常。其中任何一个都可能是它。以上是给你的+[NSDictionary(JSONCategories) dictionaryWithContentsOfJSONURLString:];如果异常来自fetchedData:,那么这意味着您正在调用fetchedData:nil并且您应该编辑您的问题以显示调用的代码fetchedData:以及该代码如何获取(或尝试获取)数据。

于 2012-05-06T18:56:20.907 回答