0

我正在尝试使用 Json-Framework 从我的网站解析输出(在 iPhone 上)。网站上的数据是一组新闻对象...通过 PHP 的 json_encode() 传递。

JSON 输出在这里: http: //pastebin.com/Be429nHx

我能够连接到服务器并存储 JSON 数据,然后执行以下操作:

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

// Create a dictionary from the JSON string
NSDictionary *results = [jsonString JSONValue];

NSArray *items = [results valueForKeyPath:@"data.array"];
NSLog(@" NewsID : %@", [[items objectAtIndex:1] objectForKey:@"newsID"]);

我从 NSLog() 行收到以下错误:

-JSONValue failed. Error is: Didn't find full object before EOF
Event Id : (null)
STATE: <SBJsonStreamParserStateStart: 0x4e3c960>
JSONValue failed. Error is: Token 'key-value separator' not expected before outer-most array or object
Event Id : (null)

任何帮助将不胜感激......谢谢!

4

1 回答 1

1

这可能是因为您的 JSON 结构为数组,所以您应该这样做:

NSArray *items = [jsonString JSONValue];
NSLog(@" NewsID : %@", [[items objectAtIndex:1] objectForKey:@"newsID"]);

或者,或者,更改 JSON 本身,使其更像:

{"somekeyname":[your existing json array]}
于 2011-04-25T03:54:53.257 回答