Get values from NSDictionary

2019-02-26 03:35发布

问题:

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 ?

回答1:

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.
}


回答2:

First you need to parse your string.

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


回答3:

So you have JSON string and array of 2 objects. So write following code

This will convert JSON string to Array

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

Now use for loop and print data as

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"]);
}

Now there are good chances that you will get NULL for some keys and it can cause in crash. So avoid those crash by using Null validations.