2011-08-31 53 views
0

我有一個問題與JSON解析
-I建立到遠程服務器的連接,以獲得一個JSON對象,它看起來像這樣有無問題與JSON

2011-08-31 13:23:27.280 WallpaperBackground[731:40b] jsonString:{"photoset":{"id":"72157627554107638", "primary":"6095921496", "owner":"[email protected]", "ownername":"Gariya1", "photo":[{"id":"6096223569", "secret":"47081ffe65", "server":"6202", "farm":7, "title":"snakc", "isprimary":"0"}, {"id":"6095921496", "secret":"ea3b2b5076", "server":"6064", "farm":7, "title":"images", "isprimary":"1"}, 

- 在didReceiveData方法,我分配JSON字符串

代碼:

NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 

然後我建我的字典

代碼:

NSDictionary *results = [jsonString JSONValue]; 

當我乳寧這樣,在控制檯我得到這些錯誤

2011-08-31 13:23:27.284 WallpaperBackground[731:40b] -JSONValue failed. Error trace is: (
"Error Domain=org.brautaset.JSON.ErrorDomain Code=5 \"Unescaped control character '0x0'\" UserInfo=0x4e1d670 {NSLocalizedDescription=Unescaped control character '0x0'}", 
"Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Object value expected for key: server\" UserInfo=0x4e1d7c0 {NSUnderlyingError=0x4e1d6d0 \"Unescaped control character '0x0'\", NSLocalizedDescription=Object value expected for key: server}", 
"Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Expected value while parsing array\" UserInfo=0x4e1d810 {NSUnderlyingError=0x4e1d7a0 \"Object value expected for key: server\", NSLocalizedDescription=Expected value while parsing array}", 
"Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Object value expected for key: photo\" UserInfo=0x4e1d8d0 {NSUnderlyingError=0x4e1d890 \"Expected value while parsing array\", NSLocalizedDescription=Object value expected for key: photo}", 
"Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Object value expected for key: photoset\" UserInfo=0x4e1d980 {NSUnderlyingError=0x4e1d8b0 \"Object value expected for key: photo\", NSLocalizedDescription=Object value expected for key: photoset}" 

如果任何一個KNW讓我幫了PL ...感謝名單

回答

0

實例化一個NSMutableData,最好是在您的.h文件中找到的接口定義中公開的。 每次調用connection:didReceiveData:時,都使用NSMutableData's "appendData:data"方法將新接收的數據添加到變量中。

當你的connection:didFinishLoading:方法被調用時,你將有一個完全填充的NSMutableData對象等待被解析。 然後調用SBJSONParser's方法來解析它,就像你一樣。

編輯

你在你的.h文件中聲明NSMutableData對象。

每當你的連接對象調用其委託的方法DidReceiveData您將追加數據如下:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [publicDataObject appendData:data]; 
} 

當數據被完全下載就只留下一個對象在它的完整的內容。 所以後來在finishedLoading方法:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSString *jsonString = [[NSString alloc] initWithData:publicDataObject encoding:NSUTF8StringEncoding]; 

    id results = [jsonString JSONValue]; 
} 

如果你肯定知道是什麼造成的數據類型將是您可以將結果變量的數據類型從ID到即的NSDictionary,NSArray的或改變。

希望這會有所幫助。

+0

ü可以給我舉例與該 –

+0

檢查我的編輯;) – Craimasjien

+0

感謝名單花花公子的ANS –

0

的方法將在部分數據加載時被多次調用。在調用connection:didFinishLoading:之前,您不能認爲數據是完整的。在此之前,您必須連接緩衝區中的數據塊(如NSMutableData)。

+0

thanku何苦多 –