Как связаться с json с iOS
Я хочу достичь от ios до веб-службы.net.asmx, и я достигаю следующим кодом:
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"}
но я не могу связаться с членами и оценивать по одному
Как я могу это сделать?
Вы можете конвертировать данные 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"
}
Затем вы можете получить ключи и значения, как и любой другой словарь.