我正在尝试使用foursquare api来附近的地方。 这是从
返回的json数据 NSDictionary *results = [jsonString JSONValue];
NSLog(@"%@", results);
(
{
code = 200;
errorDetail = "This endpoint will stop returning groups in the future. Please use a current version, see http://bit.ly/lZx3NU.";
errorType = deprecated;
},
{
groups = (
{
items = (
{
categories = (
{
icon = "https://foursquare.com/img/categories/parks_outdoors/default.png";
id = 4bf58dd8d48988d163941735;
name = Park;
parents = (
"Great Outdoors"
);
pluralName = Parks;
primary = 1;
shortName = Park;
}
);
然后我尝试使用
获取数组中的组列表NSArray *groups = [ (NSDictionary *)results objectForKey:@"groups"];
返回以下错误
2011-11-05 11:42:12.907 XperienzApp[1972:207] No of results returned: 0 Results : (null)
2011-11-05 11:42:13.225 XperienzApp[1972:207] -JSONValue failed. Error trace is: (
"Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Unrecognised leading character\" UserInfo=0x5849cd0 {NSLocalizedDescription=Unrecognised leading character}"
)
2011-11-05 11:42:13.225 XperienzApp[1972:207] No of results returned: 0 Results : (null)
我应该如何解析这个?
编辑: 我尝试了建议的技术,这给了我一个数组
id groups = [[(NSDictionary *)results objectForKey:@"response"] objectForKey:@"groups"];
if ([results count] > 1){
NSLog(@"groups class %@\ngroups %@ %d", groups, [groups class], [groups count]);
日志输出格式为:
{
categories = (
{
icon = "https://foursquare.com/img/categories/nightlife/danceparty.png";
id = 4bf58dd8d48988d11f941735;
name = Nightclub;
parents = (
"Nightlife Spots"
);
pluralName = Nightclubs;
primary = 1;
shortName = Nightclub;
}
);
contact = {
};
hereNow = {
count = 0;
};
id = 4eb33ba561af0dda8f673c1b;
location = {
address = "144 Willow St 4R";
city = Brooklyn;
crossStreet = Pierrepont;
distance = 462;
lat = "40.696864";
lng = "-73.996409";
postalCode = 11201;
state = NY;
};
name = "Entertainment 720, Ltd.";
stats = {
checkinsCount = 3;
tipCount = 0;
usersCount = 1;
};
verified = 0;
}
);
name = Nearby;
type = nearby;
}
)
groups __NSArrayM 1
这也不是json并且难以解析,我如何在json中获取输出。
答案 0 :(得分:3)
我是foursquare上的iPhone主角。我会试着去试试这里发生的事情。
首先,我强烈建议您使用JSONKit作为解析器。它重量轻,速度极快:https://github.com/johnezang/JSONKit
您似乎正在正确解析JSON并正确获取字典。然后,您将记录解析的对象,而不是原始的JSON。您看到的输出是Objective-C如何选择将已解析的字典序列化为文本。绝对不是JSON。使用JSONKit,您可以将JSONString选择器发送到您的解析结果并将其转换回JSON并记录它。
如果您可以提供有关您要解决的问题的一些详细信息,我可以帮助您解决更多问题。正如Maudicus所说,请注意你要回来的错误。当我们对API进行更改时,您不希望您的应用中断。
答案 1 :(得分:1)
如果NSLog(@"%@", results);
以下的输出是您的日志语句。看来你的结果变量是一个字典对象数组。
尝试记录结果类以验证NSLog(@"%@", [results class]);
如果是数组,则groups对象是第二个对象。
if ([results count] > 1)
id groups = [results objectAtIndex:1];
NSLog(@"groups class %@\ngroups %@", [groups class], groups);
在您了解数据格式之前,请继续这样做
也是行
errorDetail = "This endpoint will stop returning groups in the future. Please use a current version, see http://bit.ly/lZx3NU.";
应引起关注。查看foursquare上的文档,了解当前获取组的方式。