从NSDictionary获取值

时间:2017-01-04 09:17:06

标签: ios objective-c nsdictionary

您好我是iOS开发的新手。我想获得响应并将这些值添加到变量中。

我尝试了但是我得到了低于响应。我不明白为什么这个回复中有斜杠。

@"[{\"VisitorID\":\"2864983a-e26b-441a-aedf-84e2a1770b8e\",\"ProfileID\":\"69c02265-abca-4716-8a2f-ac5d642f876a\",\"CompanyID\":null,\"VisitorName\":\"kanasalingam\",\"OperatorName\":\"baman\",\"Image\":null,\"isocode\":\"lk\",\"CurrentOperator\":[\"69c02265-abca-4716-8a2f-ac5d642f876a\"]},{\"VisitorID\":\"133bc108-b3bf-468a-9397-e1b0dba449db\",\"ProfileID\":\"69c02265-abca-4716-8a2f-ac5d642f876a\",\"CompanyID\":null,\"VisitorName\":\"kumar\",\"OperatorName\":\"baman\",\"Image\":null,\"isocode\":\"lk\",\"CurrentOperator\":[\"69c02265-abca-4716-8a2f-ac5d642f876a\"]}]"

我试过了:

- (void) sendOtherActiveChats:(NSDictionary *) chatDetails{
    NSLog(@"inside sendOtherActiveChats");
    NSLog(@"otherDetails  Dictionary : %@ ", chatDetails);

    NSString *VisitorID = [chatDetails objectForKey:@"VisitorID"];
    NSString *ProfileID = [chatDetails objectForKey:@"ProfileID"];
    NSString *CompanyID = [chatDetails objectForKey:@"CompanyID"];
    NSString *VisitorName = [chatDetails objectForKey:@"VisitorName"];
    NSString *OperatorName = [chatDetails objectForKey:@"OperatorName"];
    NSString *isocode = [chatDetails objectForKey:@"isocode"];

    NSLog(@"------------------------Other Active Chats -----------------------------------");
    NSLog(@"VisitorID      : %@" , VisitorID);
    NSLog(@"ProfileID      : %@" , ProfileID);
    NSLog(@"CompanyID      : %@" , CompanyID);
    NSLog(@"VisitorName    : %@" , VisitorName);
    NSLog(@"OperatorName   : %@" , OperatorName);
    NSLog(@"countryCode: %@" , isocode);
    NSLog(@"------------------------------------------------------------------------------");

}

有人可以帮助我从这个字符串中获取值吗?

3 个答案:

答案 0 :(得分:5)

您正在回复字典数组,但您的回复是字符串,因此您使用NSArray将其转换为NSJSONSerialization,就像这样将您的回复字符串转换为NSData和之后使用JSONObjectWithData:的数据从中获取数组。

NSString *jsonString = @"[{\"VisitorID\":\"2864983a-e26b-441a-aedf-84e2a1770b8e\",\"ProfileID\":\"69c02265-abca-4716-8a2f-ac5d642f876a\",\"CompanyID\":null,\"VisitorName\":\"kanasalingam\",\"OperatorName\":\"baman\",\"Image\":null,\"isocode\":\"lk\",\"CurrentOperator\":[\"69c02265-abca-4716-8a2f-ac5d642f876a\"]},{\"VisitorID\":\"133bc108-b3bf-468a-9397-e1b0dba449db\",\"ProfileID\":\"69c02265-abca-4716-8a2f-ac5d642f876a\",\"CompanyID\":null,\"VisitorName\":\"kumar\",\"OperatorName\":\"baman\",\"Image\":null,\"isocode\":\"lk\",\"CurrentOperator\":[\"69c02265-abca-4716-8a2f-ac5d642f876a\"]}]";
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

NSError *e;
NSMutableArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&e];

现在遍历数组并从中访问每个字典。

for (NSDictionary *dic in jsonArray) {
     NSLog(@"%@",[dic objectForKey:@"VisitorID"]);
     ... and so on.
}

答案 1 :(得分:1)

首先你需要解析你的字符串。

NSString *aString = @"[{\"VisitorID\":\"2864983a-e26b-441a-aedf-84e2a1770b8e\",\"ProfileID\":\"69c02265-abca-4716-8a2f-ac5d642f876a\",\"CompanyID\":null,\"VisitorName\":\"kanasalingam\",\"OperatorName\":\"baman\",\"Image\":null,\"isocode\":\"lk\",\"CurrentOperator\":[\"69c02265-abca-4716-8a2f-ac5d642f876a\"]},{\"VisitorID\":\"133bc108-b3bf-468a-9397-e1b0dba449db\",\"ProfileID\":\"69c02265-abca-4716-8a2f-ac5d642f876a\",\"CompanyID\":null,\"VisitorName\":\"kumar\",\"OperatorName\":\"baman\",\"Image\":null,\"isocode\":\"lk\",\"CurrentOperator\":[\"69c02265-abca-4716-8a2f-ac5d642f876a\"]}]";

NSData *data = [aString dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@",[[json objectAtIndex:0] objectForKey:@"VisitorID"]);

答案 2 :(得分:1)

所以你有2个对象的JSON字符串和array。所以写下面的代码

这会将JSON字符串转换为Array

NSData *myJSONData = [YOUR_JSON_STRING dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSMutableArray *arrayResponse = [NSJSONSerialization JSONObjectWithData:myJSONData options:NSJSONReadingMutableContainers error:&error];

现在用于循环和打印数据

for (int i = 0; i < arrayResponse.count; i++) {
    NSDictionary *dictionaryTemp = [arrayResponse objectAtIndex:i];
    NSLog(@"VisitorID : %@",[dictionaryTemp valueForKey:@"VisitorID"]);
    NSLog(@"ProfileID : %@",[dictionaryTemp valueForKey:@"ProfileID"]);
    NSLog(@"CompanyID : %@",[dictionaryTemp valueForKey:@"CompanyID"]);
    NSLog(@"VisitorName : %@",[dictionaryTemp valueForKey:@"VisitorName"]);
}

现在很有可能你会为某些键获得NULL并且它可能导致崩溃。因此,请使用Null validations来避免这些崩溃。