使用JSONValue获取JSON内容时出现问题 [英] Problem getting JSON contents with JSONValue

查看:1489
本文介绍了使用JSONValue获取JSON内容时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用objective-c json框架从灯塔api解析一些json - http:/ /stig.github.com/json-framework/

I am using the objective-c json framework to parse some json from the lighthouse api - http://stig.github.com/json-framework/

我之前使用过该框架与其他api没有任何问题,虽然我似乎已经来到尝试使用JSONValue从灯塔api获取结果时死路一条。

I've used the framework before with other apis with no issues although i seem to have come to a dead end when trying to grab the results from the lighthouse api using JSONValue.

似乎错误地返回了值:

NSArray *results = [json_string JSONValue];

for (NSDictionary *project in results){
    NSLog(@"project found");
}

此循环仅运行一次,但我知道至少有7个对象可供使用在JSON字符串中进行迭代。 project也被设置为字符串而不是NSDictionary,我知道这是因为在项目上调用objectForKey会导致错误。

This loop only runs once although i know there are atleast 7 objects for it to itterate through in the JSON string. project is also being set as a string and not a NSDictionary, i know this as calling objectForKey on project causes an error.

[NSCFString objectForKey:]: unrecognized selector sent to instance 

我在这里很难过,希望这不是从灯塔api返回的字符串的问题,我只是试图弄错内容,我的json字符串在这里: http://pastie.org/1390233

I'm pretty stumped here and hope this isn't an issue with the string being returned from the Lighthouse api and i am just trying to get the contents incorrectly, my json string is here: http://pastie.org/1390233

推荐答案

解析器的行为是正确的。字典结果只包含一个名为 projects 的键,它是一个数组。要遍历每个单独的项目,您需要枚举此项目属性。

The parser's behavior is correct. The dictionary results contains only 1 key with the name projects which is an array. To loop through each individual project, you need to enumerate this projects property.

NSArray *projects = [results objectForKey:@"projects"];

for(NSDictionary *item in projects) {
    NSDictionary *project = [item objectForKey:@"project"];
    // now project should have the desired keys
}

这篇关于使用JSONValue获取JSON内容时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆