我在理解从URL获取带有mantle.h的JSON文件所需的内容时遇到了一些麻烦。
有人能举例说明它是如何运作的吗?
例如: - 我有一个带有JSONFile的URL www.example.com,如下所示:
{
"name": "michael"
}
我怎么能拿到它?
答案 0 :(得分:4)
我使用此过程来获取JSON:
NSURL *s = url;//Put your desird url here
NSURLRequest *requestURL = [NSURLRequest requestWithURL:s cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.00];
NSHTTPURLResponse *response;
NSError *error = [[NSError alloc]init];
NSData *apiData = [NSURLConnection sendSynchronousRequest:requestURL returningResponse:&response error:&error];
dictionaryData = [NSJSONSerialization JSONObjectWithData:apiData options:kNilOptions error:&error];
现在dictionaryData
包含您的JSON。您可以通过以下方式获取它:
NSString *name = [dictionaryData valueForKey:@"name"];
并确保您正在进行异步请求。为此,将代码放在此块中:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
//Put the code here
});
希望这会有所帮助.. :)
答案 1 :(得分:0)
使用以下方法调用
[super getRequestDataWithURL:urlString
andRequestName:sometext];
如果成功
,您将在以下方法中获得回复- (void)successWithRequest:(AFHTTPRequestOperation *)operation withRespose:(id)responseObject withRequestName:(NSString *)requestName {
NSString *response = operation.responseString;
id jsonObject = [response objectFromJSONString];
if(![super checkforServerRequestFailureErrorMessage:jsonObject]) {
[self.leaderboardProxyDelegate leaderboardListSuccessful:jsonObject];
}
}
您将在jsonObject
中获取字典