3

我已经从 JSON 文件创建了一个 NSDictionary,但是当我尝试这样做时

NSString *key = [NSString stringWithFormat:@"%i",indexPath.row];
NSDictionary *currentObject = [JSONdata objectForKey:key];

我错误接收和错误-[__NSCFArray objectForKey:]: unrecognized selector sent to instance

当我这样做NSLog时,JSONdata这是我的输出:

(
    {
        1 =         {
            description = "";
            facets =             (
                                {
                    name = Red;
                },
                                {
                    name = Blue;
                },
                                {
                    name = Skinny;
                },
                                {
                    name = Standard;
                },
                                {
                    name = "Navy Blue";
                }
            );
            id = 1073;
            owner = 1001;
            "post_date" = 1341980987;
            transaction = 24;
            username = TonyB;
        };
    },
    {
        2 =         {
            description = "";
            facets =             (
                                {
                    name = "Bow Tie";
                },
                                {
                    name = Blue;
                },
                                {
                    name = Orange;
                },
                                {
                    name = Yellow;
                }
            );
            id = 1001;
            owner = 1001;
            "post_date" = 1340640012;
            transaction = 6;
            username = TonyB;
        };
    }
)

我是否更正认为我应该能够获得的第一组密钥应该是12使用[JSONdata objectForKey:@"0"]or [JSONdata objectForKey:@"1"]?还是我错过了什么?

4

4 回答 4

3

您的 JSON 的结构如下

包含 NSDictionary 的 NSArray

所以你需要这样做:[[JSONdata objectAtIndex:0] objectForKey:@"1"]


此外,当您有疑问时,您可以随时这样做:
NSLog(@"JSONdata class == %@", [JSONdata class]);

当你点击一个NSDictionary时,你总是有这个方法:- (NSArray *)allKeys它将返回一个包含该字典所有键的数组。

于 2012-12-07T04:38:05.137 回答
2

你有NSArray然后NSDictionary。此外,您发送keyNSString位置是intjson 中的。会是这样吗?

这有帮助吗[[JSONdata objectAtIndex:0] objectForKey:@"1"];

于 2012-12-07T04:37:15.833 回答
0

试试这个,

NSArray *currentObject = [JSONdata valueForKey:key];

您的 JSONdata 是一个数组而不是字典。所以那没有任何objectForKey方法。上面的将调用valueForKey其对象并将结果作为数组返回。如果你只想访问第一个对象,你也可以直接使用[[JSONdata objectAtIndex:0] objectForKey:key];。如果使用上述的,只需使用

NSDictionary *currentDict = [currentObject objectAtIndex:0];

理想情况下,一个简单的解决方案如下所示,

NSString *key = [NSString stringWithFormat:@"%i",indexPath.row];
NSDictionary *currentObject = [[JSONdata objectAtIndex:(indexPath.row - 1)] valueForKey:key];
于 2012-12-07T04:39:12.597 回答
0

这是一个非常奇怪的 JSON 布局。这是一个字典数组,其中每个字典都有一个键(每个键都不同),其对象是字典。我建议一个简单的字典数组。

因此,您可能拥有以下 PHP。我假设您正在从数据库或其他东西中读取数据,但它会让您了解我建议的 JSON 结构,这只是一个字典数组。我也简化facets了,但你是否走得那么远取决于你。但关键问题是 PHP 只是生成一个简单的字典数组:

<?php

    $object1 = array(
                   "description" => "", 
                   "facets" => array (
                       "Red",
                       "Blue",
                       "Skinny",
                       "Standard",
                       "Navy Blue"
                    ),
                    "id" => 1073,
                    "owner" => 1001,
                    "post_date" => 1341980987,
                    "transaction" => 24,
                    "username" => "TonyB");

    $object2 = array(
                   "description" => "", 
                   "facets" => array (
                       "Bow tie",
                       "Blue",
                       "Orange",
                       "Yellow"
                    ),
                    "id" => 1071,
                    "owner" => 1001,
                    "post_date" => 1340640012,
                    "transaction" => 6,
                    "username" => "TonyB");

    $results = array($object1, $object2);

    echo json_encode($results);

?>

这将生成以下 JSON(我已经美化了):

[
    {
        "description" : "",
        "facets" : [
             "Red",
             "Blue",
             "Skinny",
             "Standard",
             "Navy Blue"
        ],
        "id" : 1073,
        "owner" : 1001,
        "post_date" : 1341980987,
        "transaction" : 24,
        "username" : "TonyB"
    },
    {
        "description" : "",
        "facets" : [
             "Bow tie",
             "Blue",
             "Orange",
             "Yellow"
        ],
        "id" : 1071,
        "owner" : 1001,
        "post_date" : 1340640012,
        "transaction" : 6,
        "username" : "TonyB"
    }
]

然后,您可以以标准方式阅读此 JSON:

NSData *data = [NSData dataWithContentsOfURL:url];

NSError *error;
NSArray *jsonData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

最后,您的cellForRowAtIndexPathordidSelectRowAtIndexPath现在可以简单地执行以下操作:

NSDictionary *currentObject = jsonData[indexPath.row];

或者,如果您使用的 Xcode 版本早于 4.5 版,您会:

NSDictionary *currentObject = [jsonData objectAtIndex:indexPath.row];
于 2012-12-07T04:43:44.487 回答