1

接口地址: http://suggest.taobao.com/sug?area=etao&code=utf-8&callback=KISSY.Suggest.callback&q=iphone

返回:</p>

KISSY.Suggest.callback({"result": [["iphone4s", "9809"], ["iphone5", "13312"], ["iphone4 手机", "69494400"], ["iphone5 港行", "14267"], ["iphone5三网", "2271160"], ["iphone4手机壳", "6199679"], ["iphone 5手机壳", "2527284"], ["iphone 5 保护壳", "5727586"], ["iphone 4贴膜", "147271"], ["iphone5壳", "2628540"]]})


NSURL * url = [NSURL URLWithString:@"http://suggest.taobao.com/sug?area=etao&code=utf-8&callback=KISSY.Suggest.callback&q=iphone"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

NSHTTPURLResponse* urlResponse = nil;

NSError * error = nil;

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];

NSData *date = [NSData alloc]init

SBJsonParser *jsonParser = [[SBJsonParser alloc] init];


// NSMutableArray *array=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
    NSMutableArray *array = [jsonParser objectWithData:responseData];

    NSLog(@"%@",array);

此数组为空。我不知道原因。

4

3 回答 3

1

当我提到你的请求 URL 时,它有回调,如果你保留它,它不会返回你的 json 作为响应,所以从你的 URL 中删除“&callback=KISSY.Suggest.callback”

// Make sure you have include SBJSON files in your Project, as well you have imported header in your View Controller
#import "JSON.h"

// your request URL
NSURL * url = [NSURL URLWithString:@"http://suggest.taobao.com/sug?area=etao&code=utf-8&q=iphone"];

// URL Request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

NSHTTPURLResponse* urlResponse = nil;

NSError * error = nil;

// initiate Request to get Data
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];

// Encode your Response
NSString *content = [[NSString alloc] initWithBytes:[responseData bytes] length:[responseData length] encoding:NSUTF8StringEncoding];

// Now read a Dictionary from it using SBJSON Parser
NSDictionary *responseDict = [content JSONValue];

NSLog(@"Response [%@]",responseDict);
于 2013-03-28T06:39:27.747 回答
0

我不熟悉 SBJsonParser,但返回字符串的格式看起来像 JSONP,而不是 JSON。我想简单地清理包装器调用会让你得到你想要的。

另外,请注意,您的响应的“根”是字典,而不是数组。

{"result": [[...

意味着代码可能看起来像这样:

NSDictionary *response = //... decode
NSArray *results = [response objectForKey:@"result"];
于 2013-03-28T05:43:56.283 回答
0

已编辑

您只需要使用http://suggest.taobao.com/sug?area=etao&code=utf-8&q=iphone而不是http://suggest.taobao.com/sug?area=etao&code=utf-8&callback=KISSY。 Suggest.callback&q=iphone你自己的代码可以工作..

于 2013-03-28T06:08:10.843 回答