猿问
下载APP

如何用Object-C解析JSON?

如何用Object-C解析JSON?

我对iPhone很陌生。有人能告诉我解析这些数据并获取活动细节、名字和姓氏所遵循的步骤吗?

{
    "#error": false, 
    "#data": {
        "": {
            "activity_id": "35336", 
            "user_id": "1", 
            "user_first_name": "Chandra Bhusan", 
            "user_last_name": "Pandey", 
            "time": "1300870420", 
            "activity_details": "Good\n", 
            "activity_type": "status_update", 
            "photo_url": "http://184.73.155.44/hcl-meme/QA_TEST/sites/default/files/pictures/picture-1627435117.jpg"
        }, 
        "boolean": "1", 
        "1": {
            "1": {
                "photo_1_id": "9755"
            }, 
            "activity_id": "35294", 
            "album_name": "Kalai_new_Gallery", 
            "user_id": "31", 
            "album_id": "9754", 
            "user_first_name": "Kalaiyarasan", 
            "user_last_name": "Balu", 
            "0": {
                "photo_0_id": "9756"
            }, 
            "time": "1300365758", 
            "activity_type": "photo_upload", 
            "photo_url": "http://184.73.155.44/hcl-meme/QA_TEST/"
        }, 
        "3": {
            "activity_id": "35289", 
            "user_id": "33", 
            "user_first_name": "Girija", 
            "user_last_name": "S", 
            "time": "1300279636", 
            "activity_details": "girija Again\n", 
            "activity_type": "status_update", 
            "photo_url": "http://184.73.155.44/hcl-meme/QA_TEST/sites/default/files/pictures/picture-33-6361851323080768.jpg"
        },


汪汪一只猫
浏览 380回答 3
3回答

jeck猫

从OSXv10.7和iOS 5的角度来看,现在推荐的第一件事可能是NSJSONSerialization,苹果提供了JSON解析器。如果在运行时发现类不可用,则只使用第三方选项作为后盾。例如:NSData *returnedData = ...JSON data, probably from a web request...// probably check here that returnedData isn't nil; attempting// NSJSONSerialization with nil data raises an exception, and who// knows how your third-party library intends to react?if(NSClassFromString(@"NSJSONSerialization")){     NSError *error = nil;     id object = [NSJSONSerialization                       JSONObjectWithData:returnedData                       options:0                       error:&error];     if(error) { /* JSON was malformed, act appropriately here */ }     // the originating poster wants to deal with dictionaries;     // assuming you do too then something like this is the first     // validation step:     if([object isKindOfClass:[NSDictionary class]])     {         NSDictionary *results = object;         /* proceed with results as you like; the assignment to         an explicit NSDictionary * is artificial step to get          compile-time checking from here on down (and better autocompletion         when editing). You could have just made object an NSDictionary *         in the first place but stylistically you might prefer to keep         the question of type open until it's confirmed */     }     else     {         /* there's no guarantee that the outermost object in a JSON         packet will be a dictionary; if we get here then it wasn't,         so 'object' shouldn't be treated as an NSDictionary; probably         you need to report a suitable error condition */     }}else{     // the user is using iOS 4; we'll need to use a third-party solution.     // If you don't intend to support iOS 4 then get rid of this entire     // conditional and just jump straight to     // NSError *error = nil;     // [NSJSONSerialization JSONObjectWithData:...}

精慕HU

别再发明轮子了。使用json-框架或者类似的东西。如果您确实决定使用json框架,下面是如何将JSON字符串解析为NSDictionary:SBJsonParser* parser = [[[SBJsonParser alloc] init] autorelease];// assuming jsonString is your JSON string...NSDictionary* myDict = [parser objectWithString:jsonString];// now you can grab data out of the dictionary using objectForKey or another dictionary method

临摹微笑

NSString*&nbsp;path&nbsp;&nbsp;=&nbsp;[[NSBundle&nbsp;mainBundle]&nbsp;pathForResource:@"index"&nbsp;ofType:@"json"];//将文件内容读取到字符串中,注意编码NSUTF8StringEncoding&nbsp;防止乱码,NSString*&nbsp;jsonString&nbsp;=&nbsp;[[NSString&nbsp;alloc]&nbsp;initWithContentsOfFile:path&nbsp;encoding:NSUTF8StringEncoding&nbsp;error:nil];//将字符串写到缓冲区。NSData*&nbsp;jsonData&nbsp;=&nbsp;[jsonString&nbsp;dataUsingEncoding:NSUTF8StringEncoding];NSError&nbsp;*jsonError;id&nbsp;allKeys&nbsp;=&nbsp;[NSJSONSerialization&nbsp;JSONObjectWithData:jsonData&nbsp;options:NSJSONWritingPrettyPrinted&nbsp;error:&jsonError];for&nbsp;(int&nbsp;i=0;&nbsp;i<[allKeys&nbsp;count];&nbsp;i++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;NSDictionary&nbsp;*arrayResult&nbsp;=&nbsp;[allKeys&nbsp;objectAtIndex:i]; &nbsp;&nbsp;&nbsp;&nbsp;NSLog(@"name=%@",[arrayResult&nbsp;objectForKey:@"storyboardName"]);}档案:&nbsp;[ &nbsp;&nbsp;{ &nbsp;&nbsp;"ID":1, &nbsp;&nbsp;"idSort"&nbsp;:&nbsp;0, &nbsp;&nbsp;"deleted":0, &nbsp;&nbsp;"storyboardName"&nbsp;:&nbsp;"MLMember", &nbsp;&nbsp;"dispalyTitle"&nbsp;:&nbsp;"76.360779", &nbsp;&nbsp;"rightLevel"&nbsp;:&nbsp;"10.010490", &nbsp;&nbsp;"showTabBar"&nbsp;:&nbsp;1, &nbsp;&nbsp;"openWeb"&nbsp;:&nbsp;0, &nbsp;&nbsp;"webUrl":"" &nbsp;&nbsp;}, &nbsp;&nbsp;{ &nbsp;&nbsp;"ID":1, &nbsp;&nbsp;"idSort"&nbsp;:&nbsp;0, &nbsp;&nbsp;"deleted":0, &nbsp;&nbsp;"storyboardName"&nbsp;:&nbsp;"0.00", &nbsp;&nbsp;"dispalyTitle"&nbsp;:&nbsp;"76.360779", &nbsp;&nbsp;"rightLevel"&nbsp;:&nbsp;"10.010490", &nbsp;&nbsp;"showTabBar"&nbsp;:&nbsp;1, &nbsp;&nbsp;"openWeb"&nbsp;:&nbsp;0, &nbsp;&nbsp;"webUrl":"" &nbsp;&nbsp;} &nbsp;&nbsp;]
打开App,查看更多内容
随时随地看视频慕课网APP
我要回答