如何从Web服务器更新JSON文件

我有我的本地数据在json文件中,如:

{ "locations": [ { "title": "The Pump Room", "place": "Bath", "latitude": 51.38131, "longitude": -2.35959, "information": "The Pump Room Restaurant in Bath is one of the city's most elegant places to enjoy stylish, Modern-British cuisine.", "telephone": "+44 (0)1225 444477", "visited" : true }, { "title": "The Eye", "place": "London", "latitude": 51.502866, "longitude": -0.119483, "information": "At 135m, the London Eye is the world's largest cantilevered observation wheel. It was designed by Marks Barfield Architects and launched in 2000.", "telephone": "+44 (0)8717 813000", "visited" : false }, { "title": "Chalice Well", "place": "Glastonbury", "latitude": 51.143669, "longitude": -2.706782, "information": "Chalice Well is one of Britain's most ancient wells, nestling in the Vale of Avalon between the famous Glastonbury Tor and Chalice Hill.", "telephone": "+44 (0)1458 831154", "visited" : true } ] } 

我想更新每当刷新button触摸的Web服务器上的JSON文件?

总体思路是从服务器刷新本地数据,并在没有互联网连接的情况下使用它

请帮忙…

最合适的解决scheme取决于您的确切要求。 例如:

  • 你需要authentication吗?
  • 你需要在后台模式下加载JSON吗?
  • 你的JSON是巨大的,所以把它加载到内存中对系统来说不是那么好吗?
  • 你是否需要取消一个正在运行的请求,因为有可能会停顿或需要太长时间?
  • 你需要同时加载多个请求吗?

还有一些。

如果您可以用“否”回答所有要求,那么最简单的方法就足够了:

您可以在asynchronous版本中使用NSURLConnection的便捷类方法

+ (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler

你可以在这里find更多的信息: 使用NSURL连接

此外Cocoa和Cocoa Touch在NSURLConnectionNSURLSession提供了更多的先进技术来完成这个任务。 您可以在官方文档URL加载系统编程指南中阅读更多内容。

这里简单介绍一下如何使用asynchronous便捷类方法: sendAsynchronousRequest:queue:completionHandler: ::

 // Create and setup the request NSMutableURLRequest* urlRequest = [NSURLRequest requestWithURL:url]; [urlRequest setValue: @"application/json; charset=utf-8" forHTTPHeaderField:@"Accept"]; // let the handler execute on the background, create a NSOperation queue: NSOperationQueue* queue = [[NSOperationQueue alloc] init]; [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse* response, NSData* data, NSError* error) { if (data) { // check status code, and optionally MIME type if ( [(NSHTTPURLResponse*)(response) statusCode] == 200 /* OK */) { NSError* error; // here, you might want to save the JSON to a file, eg: // Notice: our JSON is in UTF-8, since we explicitly requested this // in the request header: if (![data writeToFile:path_to_file options:0 error:&error]) { [self handleError:err]; // execute on main thread! return; } // then, process the JSON to get a JSON object: id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; ... // additionally steps may follow if (jsonObject) { // now execute subsequent steps on the main queue: dispatch_async(dispatch_get_main_queue(), ^{ self.places = jsonObject; }); } else { [self handleError:err]; // execute on main thread! } } else { // status code indicates error, or didn't receive type of data requested NSError* err = [NSError errorWithDomain:...]; [self handleError:err]; // execute on main thread! } } else { // request failed - error contains info about the failure [self handleError:error]; // execute on main thread! } }]; 

另见: [NSData] writeToURL:options:error

编辑:

handleError:应执行如下:

 - (void) handlerError:(NSError*)error { dispatch_async(dispatch_get_main_queue(), ^{ [self doHandleError:error]; }); } 

这可以确保当您显示一个UIAlertView时,您的UIKit方法将在主线程上执行。