从NSDictionary获取值

Sat*_*man 2 objective-c nsdictionary ios

嗨,我是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\"]}]"
Run Code Online (Sandbox Code Playgroud)

我试过这个:

- (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(@"------------------------------------------------------------------------------");

}
Run Code Online (Sandbox Code Playgroud)

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

Nir*_*v D 5

您正在获取Array of Dictionary作为响应,但您的响应是字符串,因此您将其转换为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];
Run Code Online (Sandbox Code Playgroud)

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

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