【发布时间】:2012-07-27 20:01:20
【问题描述】:
更新
似乎 resultField 是没有价值的。将对此进行调查,但我仍然希望得到任何建议。
原帖
我的任务是开发一个基本的 iOS 应用程序,该应用程序只需发出搜索请求,然后显示大学工作的结果。我尝试使用 Google 自定义搜索引擎,但无法让它在 iPhone 上运行,所以我不得不求助于已弃用的 Google Web Search API(讲师对此表示同意)。
现在,我可以发出请求,它会按预期返回 JSON 数据,我想我现在必须解析这些数据。遗憾的是,我只有一周的时间来做这件事,这太疯狂了,因为我以前从未使用过 JSON。
如果有人可以帮助我了解如何获得 JSON 数据的基本解析,请提供一两个指导。
我在 Stackoverflow 上环顾四周,发现了一些可能有用的东西,例如所选答案 here 中的分解结构。
这个人把它放在一起,当我在代码中显示时,这对我来说很有意义:
很好的结构解释
dictionary (top-level)
sethostname (array of dictionaries)
dictionary (array element)
msgs (string)
status (number)
statusmsg (string)
warns (array)
??? (array element)
遗憾的是,我什至无法开始对我的应用程序中生成的代码做同样的事情。它采用类似于此示例代码的形式,由 google 提供 - 我不是 Paris Hilton 粉丝!
来自 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;
现在我遇到的主要问题是结果数组一直为空。我真的可以在这里朝着正确的方向前进。谢谢。
【问题讨论】:
-
你检查过你的各种对象的值了吗?我猜您的 responseData 已正确填充,但字典应该是什么?
-
好点,谢谢。我检查了不同变量的值,resultField 是最终为空的那个。这给了我一些工作,谢谢。
标签: ios json nsdictionary