Search code examples
objective-cwcfwcf-data-servicesnsjsonserialization

objective c parsing returned json from wcf service


So I asked this question recently and I was able to make it work

objective c calling wcf rest service request

Here's my code

NSString *urlStringRequest = [NSString stringWithFormat:@"http://service.mydomain.com/UserAccountService.svc/UserLogin?id=%@&pword=%@", email, incPassword];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStringRequest]];
    [request setHTTPMethod:@"GET"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

    NSError *error = nil;
    NSURLResponse *response = nil;
    NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    id jsonResult = [self parseJsonResult:result];

    NSLog(@"Json Result Woop: %@", jsonResult);

the parseJsonResult is exactly the same as the one that was on the link. My problem is though, whenever I would show the jsonResult on the NSLog, it would show the json result which is:

Json Result Woop: {
    Authenticated = 0;
    Message = "Invalid Log In.";
}

But I'm not really sure how I can go into the variable and retrieve the key and it's value one by one.

I tried to do this:

NSError *jsonParseError = nil;
NSMutableArray *jsonArray = [NSJSONSerialization JSONObjectWithData:result options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:&jsonParseError];
if(!jsonArray)
    NSLog(@"Parse error: %@", jsonParseError);
else
    for(NSDictionary *item in jsonArray)
        NSLog(@"%@", item);

using the result variable but it would just give me the keys.

Not really sure what to do.


Solution

  • JsonResult is already an NSDictionary as evidenced by NSLog showing it like this with curly braces. Just use

    jsonResult[@"Message"]
    

    to get to the value for the Message key.