iphone - json返回格式

标签 iphone ios json

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

返回:

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);

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

最佳答案

因为我提到你请求的 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);

关于iphone - json返回格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15674872/

相关文章:

java - Jolt 转换 JSON 数组保留其余字段

android - 如何在android中检索联系人列表

iphone - 如何将文本的第一个字母大写

iphone - objective-C : How can I freeze the screen and add loading animation?

ios - 如何将 UIView 链接到 UICollectionViewCell?

ios - 发布旋转 UIImage

ios - 你如何获得 iPhone 的设备名称

javascript - 克隆适合多个类别的 JSON 对象并按这些类别分组

iphone - UITextView 上的 CGAffineTransformMakeScale

ios - 如何更改 MFMailComposeViewController 的标题颜色?