nsdictionary获取值,从NSDictionary获取值

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\"]}]"

I tried this :

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

}

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

解决方案

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.

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值