objective c解析从wcf服务返回的json

时间:2013-01-15 06:19:49

标签: objective-c wcf wcf-data-services nsjsonserialization

所以我最近问过这个问题,我能够让它发挥作用

objective c calling wcf rest service request

这是我的代码

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

parseJsonResult与链接上的那个完全相同。我的问题是,每当我在NSLog上显示 jsonResult 时,它会显示json结果:

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

但我不确定如何进入变量并逐个检索密钥和值。

我试着这样做:

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

使用结果变量,但它只会给我钥匙。

不确定该怎么做。

1 个答案:

答案 0 :(得分:1)

JsonResult已经是一个NSDictionary,正如NSLog所证明的那样用花括号来表示它。只需使用

jsonResult[@"Message"]

获取Message键的值。