我遇到一個錯誤使用SBJSON解析器對一個響應字符串從PHP web服務返回:陣列從SBJSON解析器返回無效(IOS)
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSError *error;
SBJSON *json = [SBJSON new];
NSArray *returnedFromWeb = [json objectWithString:responseString error:&error];
[articles_parsed removeAllObjects];
for (int i = 0; i < [returnedFromWeb count]; i++)
{
NSDictionary *dictColumns = [returnedFromWeb objectAtIndex:i];
[self parseArticlesObject:dictColumns];
dictColumns = nil;
}
我接收無法識別的選擇發送到實例錯誤上面調用objectAtIndex。當我檢查數組中包含的值(returnedFromWeb)時,它顯示有17個對象。然而,檢查每個對象是否顯示以下錯誤:
(<invalid>) [0] = <error: expected ']' error: 1 errors parsing expression >
我已經發布數據下面從SBJSON解析器在索引返回的對象[0]下面:
{
0 = {
author = "CCJ Staff";
category1 = Products;
excerpt = "Toyo";
modified = "2014-03-25 08:33:57";
"short_link" = "http://www.example.com/?p=95326";
thumbnail = "http://www.example.com/files/2014/03/ Tire.jpg";
"thumbnail_243x243" = "http://www.example.com/ihz.php?src=/blogs.dir/10/files/2014/03/ Tire.jpg&w=243&h=243&q=100&zc=1";
title = "medium-duty";
};
這裏是字符串正在由SBJSON解析(縮短到只包含的第一個對象,在最後的逗號是下一個目標將開始):
{"numberposts":240,"tax_query":[{"taxonomy":"topic","terms":[75,43550,14394,16564,1795,43,22,12950],"field":"term_id","orderby":"date","order":"DESC"}],"0":{"title":"medium-duty","category1":"Products", excerpt":"Toyo","modified":"2014-03-25 08:33:57","author":"Staff","thumbnail":"http://www.ccjdigital.com/files/2014/03/Tire.jpg","short_link":"http://www.ccjdigital.com/?p=95326"},
任何幫助表示讚賞!我對iOS和SBJSON相當陌生,並且一直無法弄清楚爲什麼我的數組無效。
您是否驗證過您從Web服務器接收的json以確保其格式正確? –
您的returnedFromWeb可能不是NSArray,而是NSDictionary。 – streem
另外,'objectWithString'消息會給你一個...一個對象,而不是一個數組。 –