0

我正在调用第三方 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了以下参数:kNilOptionsNSJSONReadingMutableContainers |NSJSONReadingAllowFragments我仍然得到零。

转换NSDataNSString谴责结果是 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 或更好地提取我需要的数据?

4

1 回答 1

1

首先,您必须始终明确了解您的 REST 资源返回的数据类型(即,而不是像您正在做的那样反复试验),使用 postman 等工具您提供content-type响应的标题:

在此处输入图像描述

使用该信息,并使用AFNetworking(这使得此类任务更容易),您可以像这样注册您的响应类型:

AFHTTPClient *client = 
    [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:kApiBaseUrl]];
// notice that request parameter encoding may very well be of type json, that's 
// not the same as the return header being json or html etc
client.parameterEncoding = AFJSONParameterEncoding;
// here you are setting the return header as 'text/html' b/c that's what 
// exactly what it is, if postman said it was of type 'application/json' 
// then you'd put that here
[client setDefaultHeader:@"Accept" value:@"text/html"];

然后你可以发布得到这样的:

NSString *postPath = @"your api resource path";
NSDictionary *params = @{@"param1name": param1value,
                         @"param2name": param2value}; // your request params

[client postpath:postPath parameters:params 
       success:^(AFHTTPRequestOperation *operation, id JSON){
   // here you convert the JSON into an array like structure
   id payload = [NSJSONSerialization JSONObjectWithData:JSON 
                                                options:NSJSONReadingAllowFragments 
                                                  error:nil];
   // process payload
   NSString *fname = payload[@"fname"]; //etc
}failure:^(AFHTTPRequestOperation *operation, NSError *error){
   // handle error
}

我建议您阅读JSONObjectWithData:options:error:的文档,尤其是选项部分。

于 2013-10-16T02:00:05.280 回答