访问未知字典键的值

发布于 2024-12-29 10:09:44 字数 942 浏览 0 评论 0原文

我的 JSON 响应如下所示:

    {
    "themes": [
        [
            "Direction des Routes Secteur de Pithiviers ",
            "mairies",
            "Morailles 45300 PITHIVIERS LE VIEIL ",
            "0238345744",
            "48.823042",
            "2.365907"
        ],
        [
            "Crédit Mutuel Du Centre ",
            "banques",
            "agence de Pithiviers  33 r Am Gourdon 45300 PITHIVIERS",
            "0820834080",
            "48.703042",
            "2.145907"
        ]
    ]
}

如您所见,所有键都是未知的,我没有 Key:value,那么,我如何引用每个值。

NSDictionary *allThemesDict=[[request responseString]JSONValue];
 NSArray *allThemesValues=[allThemesDict objectForKey:@"themes"];
    for (int i=0; i<[allThemesValues count]; i++) {
        NSDictionary *currentTheme=[allThemesValues objectAtIndex:i];//here i get the first 6 values, how can i display it?
        }

My JSON response looks like this:

    {
    "themes": [
        [
            "Direction des Routes Secteur de Pithiviers ",
            "mairies",
            "Morailles 45300 PITHIVIERS LE VIEIL ",
            "0238345744",
            "48.823042",
            "2.365907"
        ],
        [
            "Crédit Mutuel Du Centre ",
            "banques",
            "agence de Pithiviers  33 r Am Gourdon 45300 PITHIVIERS",
            "0820834080",
            "48.703042",
            "2.145907"
        ]
    ]
}

As you can see, all the Keys are unknown, i don't have Key:value, so, how can i refer to each value.

NSDictionary *allThemesDict=[[request responseString]JSONValue];
 NSArray *allThemesValues=[allThemesDict objectForKey:@"themes"];
    for (int i=0; i<[allThemesValues count]; i++) {
        NSDictionary *currentTheme=[allThemesValues objectAtIndex:i];//here i get the first 6 values, how can i display it?
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遥远的绿洲 2025-01-05 10:09:44

它们是数组,而不是字典。所以你可以像这样获取数组:

NSDictionary *allThemesDict = [[request responseString] JSONValue];
NSArray *allThemesValues = [allThemesDict objectForKey:@"themes"];
for (int i=0; i<[allThemesValues count]; i++) {
    NSArray *currentTheme = [allThemesValues objectAtIndex:i];
    NSLog(@"currentTheme = %@", currentTheme);

    // Then access [currentTheme objectAtIndex:1] for example to get "mairies" in the first case.
}

They're arrays, not dictionaries. So you can just grab the array like this:

NSDictionary *allThemesDict = [[request responseString] JSONValue];
NSArray *allThemesValues = [allThemesDict objectForKey:@"themes"];
for (int i=0; i<[allThemesValues count]; i++) {
    NSArray *currentTheme = [allThemesValues objectAtIndex:i];
    NSLog(@"currentTheme = %@", currentTheme);

    // Then access [currentTheme objectAtIndex:1] for example to get "mairies" in the first case.
}
红ご颜醉 2025-01-05 10:09:44

您的示例中只有一个键:顶级字典有一个名为“主题”的键。其他一切都是数组或原语。

假设您事先不知道“themes”键将被称为“themes”,您可以通过迭代字典来发现它。您可能会遇到一个问题,如果您不知道键是什么,您可能对数据了解不够,无法预测值的类型和内容。这里的解决方案是与服务器开发人员就数据格式规范达成一致。作为参考,您可以像这样迭代字典(有不止一种方法):

for (id key in myDictionary) {
    //...
}

There's only one key in your example: the top-level dictionary has a key called "themes". Everything else is an array or a primitive.

On the assumption that you don't know in advance the "themes" key will be called "themes", you can discover it by iterating over the dictionary. You may encounter a problem in that if you don't know what the keys are, you may not know enough about the data to predict the type and content of the value. The solution here is to agree with the server developers a specification for the data format. For reference, you iterate over a dictionary like this (there's more than one way):

for (id key in myDictionary) {
    //...
}
拥抱没勇气 2025-01-05 10:09:44

您只想遍历数组吗?在这种情况下,类似这样的事情可能会起作用:

    NSDictionary *currentTheme=[allThemesValues objectAtIndex:i];
        for (id item in currentTheme) {
            NSLog(@"item: %@", item);
        }
    }

Do you simply want to iterate through the array? In that case, something like this could work:

    NSDictionary *currentTheme=[allThemesValues objectAtIndex:i];
        for (id item in currentTheme) {
            NSLog(@"item: %@", item);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文