2013-03-28 143 views
1

阿比地址: http://suggest.taobao.com/sug?area=etao&code=utf-8&callback=KISSY.Suggest.callback&q=iphoneJSON返回格式

回報:

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

這個數組爲空。我不知道原因。

+0

刪除「&回調= KISSY.Suggest.callback」我害怕它是與你的編碼和那些日本人?字符。 – Ares 2013-03-28 05:40:06

+0

沒什麼可擔心的日語,它可以解析,請參考下面的答案。只需按照說明操作即可通過。 – 2013-03-28 06:52:59

+0

謝謝你,但字符串是中國 – paul 2013-03-28 07:53:42

回答

1

因爲我指你請求的URL,它有回調,如果你保留它,它不會返回你json a的反應,所以從您的網址

// 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); 
+0

@paul你試試看嗎? – 2013-03-28 06:45:45

+0

謝謝你,我試了一下,但是resultdict = null – paul 2013-03-28 07:23:15

+0

它返回我的回覆,讓我編輯你的代碼和我的答案,只需稍等片刻 – 2013-03-28 07:23:57

0

我不熟悉的SBJsonParser,但返回的字符串的格式看起來像JSONP,JSON不是。我想可能只是簡單地清理一下包裝器的調用會讓你知道你在做什麼。

此外,請注意,你的響應的「根」是一個字典,而不是陣列。

{"result": [[... 

意味着代碼可能應該是這樣的:

NSDictionary *response = //... decode 
NSArray *results = [response objectForKey:@"result"]; 
+0

謝謝^^我也跟着你的回報是空 – paul 2013-03-28 06:20:07

0
+0

謝謝你,是的,我想是這樣,但我不知道如何擺脫以前的字符串的。 PS:我的英語不好對不起 – paul 2013-03-28 06:32:18

+0

更新我的回答,請檢查.. – iphonic 2013-03-28 06:45:41

+0

謝謝^^我一定要試穿一下 – paul 2013-03-28 07:22:01