我正在调用第三方 JSON 服务
NSString* urlString = [NSString stringWithFormat:BASE_JSON_URL, address];
NSURL* urlAddressJSonUrl = [NSURL URLWithString:urlString];
dispatch_async(globalQueue,
^{
NSData* addressData = [NSData dataWithContentsOfURL:urlAddressJSonUrl];
[self performSelectorOnMainThread:@selector(resolveFromJSonResult:)
withObject:addressData waitUntilDone:YES];
});
将 urlString 复制并粘贴到浏览器中导致状态正常,我将 JSON 对象作为字典。将NSData
结果转换NSDictionary
为如下结果为 nil
NSDictionary* jsonAddressResult = [NSJSONSerialization JSONObjectWithData:jsonResponseData options:NSJSONReadingAllowFragments error:&error];
还尝试options
了以下参数:kNilOptions
但NSJSONReadingMutableContainers |NSJSONReadingAllowFragments
我仍然得到零。
转换NSData
为NSString
谴责结果是 HTML:
(null)<style>body{color:#black;} span.q{color:#FF0084;} span.ns{color:#0259C4; font-weight:bold;} span.n{color:#666666;} span.at{font-weight:bold;}</style><pre>{ "<span class="ns">places</span>": {
"<span class="ns">place</span>": [
{ "<span class="at">place_id</span>": "<span class="q">RaZZuVZQW7xqJM2P</span>", "<span class="at">woeid</span>": "<span class="q">1968212</span>", "<span class="at">latitude</span>": <span class="n">32.045</span>, "<span class="at">longitude</span>": <span class="n">34.769</span>, "<span class="at">place_url</span>": "<span class="q">\/Spain\/Tel+Aviv\/Tel+Aviv</span>", "<span class="at">place_type</span>": "<span class="q">locality</span>", "<span class="at">place_type_id</span>": <span class="n">7</span>, "<span class="at">timezone</span>": "<span class="q">Asia\/Jerusalem</span>", "<span class="at">_content</span>": "<span class="q">Madrid, Madrid, Spain</span>", "<span class="at">woe_name</span>": "<span class="q">Madrid</span>" }
], "<span class="at">query</span>": "<span class="q">Madrid, Spain</span>", "<span class="at">total</span>": <span class="n">1</span> }, "<span class="at">stat</span>": "<span class="q">ok</span>" }</pre>
如何将其转换为 JSON 或更好地提取我需要的数据?