アップデート
resultFieldは値がないようです。これを調べますが、それでもアドバイスをいただければ幸いです。
元の投稿
私は、検索リクエストを作成し、大学の仕事の結果を表示するだけの基本的なiOSアプリケーションを考え出すというタスクを設定しました。Googleカスタム検索エンジンを使おうとしましたが、iPhoneで動作させることができなかったため、減価償却されたGoogle Web Search APIを使用する必要がありました(講師はこれで問題ありません)。
これで、リクエストを行うことができ、意図したとおりにJSONデータが返されます。これを解析する必要があると思います。悲しいことに、これを行うのに1週間しかありません。これは、これまでJSONを使用したことがないのでクレイジーです。
私が望んでいるのは、JSONデータの基本的な解析だけでも取得する方法について、誰かが1つか2つのポインターを使って私が地面から離れるのを手伝ってくれるかどうかです。
Stackoverflowを見回して、ここで選択した回答の内訳構造など、役立つ可能性のあるものをいくつか見ました。
人はこれをまとめました。これは、コードに示されている場合、私にはある程度意味があります。
素晴らしい構造の説明
dictionary (top-level)
sethostname (array of dictionaries)
dictionary (array element)
msgs (string)
status (number)
statusmsg (string)
warns (array)
??? (array element)
残念ながら、アプリで生成されたコードで同じことを始めることすらできません。これは、このサンプルコードに似た形式を取ります。これは、Googleの厚意によるものです。私はパリスヒルトンのファンではありません。
Googleのサンプルコード。
{"responseData": {
"results": [
{
"GsearchResultClass": "GwebSearch",
"unescapedUrl": "http://en.wikipedia.org/wiki/Paris_Hilton",
"url": "http://en.wikipedia.org/wiki/Paris_Hilton",
"visibleUrl": "en.wikipedia.org",
"cacheUrl": "http://www.google.com/search?q\u003dcache:TwrPfhd22hYJ:en.wikipedia.org",
"title": "\u003cb\u003eParis Hilton\u003c/b\u003e - Wikipedia, the free encyclopedia",
"titleNoFormatting": "Paris Hilton - Wikipedia, the free encyclopedia",
"content": "\[1\] In 2006, she released her debut album..."
},
{
"GsearchResultClass": "GwebSearch",
"unescapedUrl": "http://www.imdb.com/name/nm0385296/",
"url": "http://www.imdb.com/name/nm0385296/",
"visibleUrl": "www.imdb.com",
"cacheUrl": "http://www.google.com/search?q\u003dcache:1i34KkqnsooJ:www.imdb.com",
"title": "\u003cb\u003eParis Hilton\u003c/b\u003e",
"titleNoFormatting": "Paris Hilton",
"content": "Self: Zoolander. Socialite \u003cb\u003eParis Hilton\u003c/b\u003e..."
},
...
],
"cursor": {
"pages": [
{ "start": "0", "label": 1 },
{ "start": "4", "label": 2 },
{ "start": "8", "label": 3 },
{ "start": "12","label": 4 }
],
"estimatedResultCount": "59600000",
"currentPageIndex": 0,
"moreResultsUrl": "http://www.google.com/search?oe\u003dutf8\u0026ie\u003dutf8..."
}
}
, "responseDetails": null, "responseStatus": 200}
これはこれまでのコードです。すぐにわかるように、上記のコードと同様のコードを返す以外に、実際にはほとんど何もしません。
**My code.**
// query holds the search term
query = [query stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//append theQuery with search URL
NSString *tempString = [NSString stringWithFormat:@"%@/%@", @"https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=", theQuery];
//Create NSURL out of tempString
NSURL *url = [NSURL URLWithString:tempString];
// Create a request object using the URL.
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// Prepare for the response back from the server
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONWritingPrettyPrinted error:&error];
NSDictionary* resultField = [NSDictionary dictionaryWithDictionary:[dictionary objectForKey:@"results"]];
// Send a synchronous request to the server (i.e. sit and wait for the response)
// Check if an error occurred
if (error != nil) {
NSLog(@"%@", [error localizedDescription]);
// Do something to handle/advise user.
}
// Convert the response data to a string.
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSArray *results = [dictionary objectForKey:@"results"];
//set label's text value to responseString's value.
endLabel.text = responseString;
今私が遭遇した主な問題は、結果の配列が常にnullであるということです。私はここで正しい方向のポイントで本当に行うことができました。ありがとうございました。