-3

我想从 ios 访问 .net .asmx Web 服务,并使用以下代码进行访问:

NSString *urlString = @"http://www.****.com/Mobile.asmx/HelloWorld";
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod: @"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];


NSError *errorReturned = nil;
NSURLResponse *theResponse =[[NSURLResponse alloc]init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];
if (errorReturned)
{
    //...handle the error
}
else
{
    NSString *retVal = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@", retVal);
    //...do something with the returned value


}

它返回干净的 json {"hellom":"Hello World","hellom2":"HelloWorld2"},但我无法联系到成员并一一取值

我怎样才能做到这一点?

4

1 回答 1

2

您可以使用以下代码将 JSON 数据转换为对象...

NSData *jsonData = ... the data you got from the server

NSDictionary *object = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];

然后对象将是这样的 NSDictionary ...

@{
    hellom : @"Hello World",
    hellom2: @"HelloWorld2"
}

然后,您可以像任何其他字典一样获取键和值。

于 2013-07-09T13:24:46.690 回答