从Objective C中的String获取JSON URL

时间:2014-03-28 09:45:49

标签: objective-c json nsdictionary nsjsonserialization

我只是尝试在Objective-C中部署客户端服务器应用程序。当我进行查询时,我从服务器获得这样的json字符串:

@"{"url":"http://my-company.com/json"}".

如何使用Objective C直接提取the http://my-company.com/json

1 个答案:

答案 0 :(得分:1)

详细了解NSJSONSerialization

NSString *jsonString = @"{\"url\":\"http://my-company.com/json\"}";
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

NSError *e;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&e];
NSString *url = [jsonDict objectForKey:@"url"];
NSLog(@"url: %@", url);