我的NSDictionary的关键是什么?

时间:2012-12-07 04:34:40

标签: objective-c ios json nsdictionary

我已经从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"][JSONdata objectForKey:@"1"]的第一组密钥?或者我错过了什么?

4 个答案:

答案 0 :(得分:3)

您的JSON结构如下

包含NSDictionary的NSArray

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


如果有疑问,你可以随时这样做:
NSLog(@"JSONdata class == %@", [JSONdata class]);

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

答案 1 :(得分:2)

您有NSArray然后NSDictionary。您也将key发送为NSString,因为它是json中的int。可能是吗?

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

答案 2 :(得分:0)

试试这个,

NSArray *currentObject = [JSONdata valueForKey:key];

您的JSONdata是一个数组,而不是字典。所以没有任何objectForKey方法。上面的一个将调用valueForKey on its objects并将结果作为数组返回。如果您只想访问第一个对象,也可以直接使用[[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];

答案 3 :(得分: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];

最后,您的cellForRowAtIndexPathdidSelectRowAtIndexPath现在只需执行以下操作:

NSDictionary *currentObject = jsonData[indexPath.row];

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

NSDictionary *currentObject = [jsonData objectAtIndex:indexPath.row];