如何在运行时从服务器更新ios中的json文件

时间:2014-02-27 12:26:44

标签: ios json

我想更新我在应用中离线编译的json中的ios app文件。刷新应用程序时,应从服务器更新文件:localhost:8888/ios/ios_app/Service/data.json

请帮忙...... 提前谢谢

1 个答案:

答案 0 :(得分:0)

当我需要获取可以序列化为字符串类型的值时,我使用SOAP。但如果您需要的只是json文件,请查看NSURLConnection类。

- (void)downloadJSONFromURL {
    NSURLRequest *request = ....
    NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
    // ...
}

NSData *urlData;

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    urlData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [urlData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSError *jsonParsingError = nil;
    id object = [NSJSONSerialization JSONObjectWithData:urlData options:0 error:&jsonParsingError];

    if (jsonParsingError) {
        DLog(@"JSON ERROR: %@", [jsonParsingError localizedDescription]);
    } else {
        DLog(@"OBJECT: %@", [object class]);
    }
}