如何从json数组访问值

时间:2021-06-29 13:20:16

I have some NSData coming back from my server that is of type Json. I would like to know how to access the values present in the json and put them into there own NSString objects.

我有一些NSData从我的服务器返回,类型为Json。我想知道如何访问json中存在的值并将它们放入自己的NSString对象中。

this is what the structure of the JsonArray looks like

这就是JsonArray的结构

如何从json数组访问值

This is the code I am using, however my for loop only ever shows "result" and nothing else.

这是我正在使用的代码,但是我的for循环只显示“结果”而没有别的。

NSError *error = nil;
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:csvData options: NSJSONReadingMutableContainers error: &error];
    NSLog(@"%@", jsonArray);
    if (!jsonArray) {
        NSLog(@"Error parsing JSON: %@", error);
    } else {
        for(NSDictionary *item in jsonArray) {
            NSLog(@"Item: %@", item);
        }
    }

any help would be greatly appreciated.

任何帮助将不胜感激。

3 个解决方案

#1


2  

You can access it like that:

您可以这样访问它:

for (NSDictionary *dict in jsonArray)
{
    NSLog(@"Data: %@", dict[@"result"]);
}

You have dictionary in your array so you have to enumerate is and access it by key (result, etc.).

您的数组中有字典,因此您必须枚举is并按键访问它(结果等)。

#2


1  

NSDictionary *resultDictionary = [jsonArray objectAtIndex: 0];
NSArray *resultArray = [resultDictionary objectForKey:@"result"];

for (NSString *item in resultArray)
{
    NSLog (@"item: %@",item);
}

You should add some (non)sense checking.

你应该添加一些(非)意义检查。

#3


0  

u can access the json array like this,

你可以像这样访问json数组,

for(NSDictionary *Mydictionary in MyJsonArray) {
   Nsstring *DataOne = [Mydictionary objectforkey@"Mykey"];
}

For Cheking the json node u can put the json in this site and all the node will appear properly

对于Cheking json节点,你可以将json放在这个站点中,所有节点都会正确显示

http://json.parser.online.fr/

#1


2  

You can access it like that:

您可以这样访问它:

for (NSDictionary *dict in jsonArray)
{
    NSLog(@"Data: %@", dict[@"result"]);
}

You have dictionary in your array so you have to enumerate is and access it by key (result, etc.).

您的数组中有字典,因此您必须枚举is并按键访问它(结果等)。

#2


1  

NSDictionary *resultDictionary = [jsonArray objectAtIndex: 0];
NSArray *resultArray = [resultDictionary objectForKey:@"result"];

for (NSString *item in resultArray)
{
    NSLog (@"item: %@",item);
}

You should add some (non)sense checking.

你应该添加一些(非)意义检查。

#3


0  

u can access the json array like this,

你可以像这样访问json数组,

for(NSDictionary *Mydictionary in MyJsonArray) {
   Nsstring *DataOne = [Mydictionary objectforkey@"Mykey"];
}

For Cheking the json node u can put the json in this site and all the node will appear properly

对于Cheking json节点,你可以将json放在这个站点中,所有节点都会正确显示

http://json.parser.online.fr/