从NSDictionary获取值

从NSDictionary获取值

本文介绍了从NSDictionary获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hi I'm new to iOS development. I want to get response and add those values to variable.

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

I tried it but I'm getting below response. I don't understand why there is slashes in this response.

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

}

有人可以帮助我从中获取价值string?

Can some one help me to get the values out of this string ?

推荐答案

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

You are getting Array of Dictionary in response, but your response is in string so you convert it to NSArray using NSJSONSerialization like this way for that convert your response string to NSData and after that use that data with JSONObjectWithData: to get array from it.

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

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

Now loop through the array and access the each dictionary from it.

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

这篇关于从NSDictionary获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 19:01