如何在ios中处理Web服务json数据

时间:2014-09-10 05:14:37

标签: ios objective-c

我的网络服务为我提供以下json。

{"data":{"error":null,"success":true,"id":"129","userId":"dasfasf@afdsfgd.com","password":"pass123","email":"dasfasf@afdsfgd.com","name":"aasdasd","type":"candidate","img":"http:\/\/www.mo.bluehorse.in\/proximity\/admin\/images\/candidate\/defaultimg.png","industry":"Accounting \/ Finance","functionnml":"Architects \/ Interior Design \/ Naval Arch.","role":"Architect","summary":"VDFSVGDS","linkedin_id":"FSDAFSDAFDS"}}

我正试图通过

访问此内容
NSArray *logedInUser = [NSArray arrayWithArray:(NSArray *)[jsonData valueForKey:@"data"]];

我也试过

NSMutableArray *logedInUser = [NSMutableArray arrayWithArray:(NSArray *)[jsonData valueForKey:@"data"]];

我是新的iOS。请帮我从上面的json获取数据。

收到数据后,我想将其保存到NSUserDefaults *preferences 所以我的代码如下

NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
            NSString *currentLevelKey = @"logedInUser";
[preferences setObject:logedInUser forKey:currentLevelKey];
const BOOL didSave = [preferences synchronize];
if(didSave){//my own code is here}

它给我以下错误

  

*由于未捕获的异常而终止应用   'NSInvalidArgumentException',原因:'* - [NSArray   initWithArray:range:copyItems:]:数组参数不是NSArray' *

另外请告诉我这是好的还是我需要做别的事。

提前谢谢..

2 个答案:

答案 0 :(得分:0)

NSString *string = @"{\"data\": {\"error\": null,\"success\": true,\"id\": \"129\",\"userId\": \"dasfasf@afdsfgd.com\",\"password\": \"pass123\",\"email\": \"dasfasf@afdsfgd.com\",\"name\": \"aasdasd\",\"type\": \"candidate\",\"img\": \"http://www.mo.bluehorse.in/proximity/admin/images/candidate/defaultimg.png\",\"industry\": \"Accounting / Finance\",\"functionnml\": \"Architects / Interior Design / Naval Arch.\",\"role\": \"Architect\",\"summary\": \"VDFSVGDS\",\"linkedin_id\": \"FSDAFSDAFDS\"}}";

        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[string dataUsingEncoding:NSUTF8StringEncoding] options:0 error:NULL];
        NSLog(@"dict : %@",dict);
        NSMutableArray *allvalues = [NSMutableArray array];
        [allvalues addObjectsFromArray:[dict objectForKey:@""]];
        NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
        for (NSDictionary *value in [dict objectForKey:@"data"]) {
            NSLog(@"value : %@:%@",value,[[dict objectForKey:@"data"] objectForKey:value]);
            [preferences setValue:value forKey:[[dict objectForKey:@"data"] objectForKey:value]];
        }

答案 1 :(得分:0)

你以NSDictionary的形式得到jsonData而不是NSArray ...尝试下面的代码

NSDictionary *dictData = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];

NSDictionary *dataDict=[dictData objectForKey:@"data"];

NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
NSString *currentLevelKey = @"logedInUser";
[preferences setObject:dataDict forKey:currentLevelKey];
const BOOL didSave = [preferences synchronize];
if(didSave){//my own code is here}

//(OR) If your dictionary contains data other than NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. so you need to store like below

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self.selectedOptionPositions];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"logedInUser"];

//Retrieving:

NSData *dictionaryData = [preferences objectForKey:@"logedInUser"];
NSDictionary *dictionary = [NSKeyedUnarchiver unarchiveObjectWithData:dictionaryData];

希望它对你有用......!