在自學iOS程式的途中,總算需要一些server上的資料了,但是其實也只是要讀一些資料罷了 在需求相當的簡單之下,也曾經去尋找過CSV甚至是找個地方把資料寫成JSON硬讀 不過由於要能方便的修改,所以似乎使用Google Drive上面的Spreadsheet是最簡單最方便的方式

主要參考文章如下:

流程如下:

主要程式都是參考 iOS中NSJSONSerialization解析JSON数据暨google地理信息处理案例

-(void)parseJson
{
//The URL of JSON service
//NSString *_urlString = @"http://maps.googleapis.com/maps/api/geocode/json?address=nanjing&sensor=true";
NSString *_urlString = @"https://spreadsheets.google.com/feeds/list/0Artq5Bi16cQedE5mU21kdkJBWUtPU01XbS1uNW5JbEE/1/public/values?alt=json";
NSString *_dataString = [[NSString alloc] initWithData:[_urlString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES] encoding:NSASCIIStringEncoding];

//_dataString=[NSString stringWithUTF8String:[_urlString UTF8String]];

NSURL *_url = [NSURL URLWithString:_dataString];
NSMutableURLRequest *_request = [NSMutableURLRequest requestWithURL:_url];
[_request setValue:@"accept" forHTTPHeaderField:@"application/json"];
[NSURLConnection sendAsynchronousRequest:_request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse* response, NSData* data, NSError* error) {
//block define statment
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;

int responseStatusCode = [httpResponse statusCode];
NSLog(@"response status code is %d",responseStatusCode);

NSError *_errorJson = nil;

NSDictionary *resultsDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

if (_errorJson != nil)
{
NSLog(@"Error %@", [_errorJson localizedDescription]);
}
NSDictionary *resultDicFeed = [resultsDic objectForKey:@"feed"];
NSArray *resultsArryEntry= [resultDicFeed objectForKey:@"entry"];
for (NSDictionary * resultDetailDicAll in resultsArryEntry)
{
NSDictionary *resultsDicID=[resultDetailDicAll objectForKey:@"gsx$id"];
NSString * dataID=[resultsDicID objectForKey:@"$t"];

NSDictionary *resultsDicName=[resultDetailDicAll objectForKey:@"gsx$name"];
NSString * dataName=[resultsDicName objectForKey:@"$t"];

NSDictionary *resultsDicDesc=[resultDetailDicAll objectForKey:@"gsx$description"];
NSString * dataDesc=[resultsDicDesc objectForKey:@"$t"];
}

}];
}

如果你有其他欄位名稱~記得把欄位gsx$id gsx$name  gsx$description 改成你的欄位值….


Evan

Attitude is everything