【问题标题】:How to access values from json array如何访问json数组中的值
【发布时间】:2013-11-26 08:46:29
【问题描述】:

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

JsonArray 的结构是这样的

这是我正在使用的代码,但是我的 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);
        }
    }

【问题讨论】:

  • 看起来您的数组包含字典。你的字典有一个字符串作为键,另一个数组作为值。休息由你决定

标签: ios objective-c json nsarray


【解决方案1】:

你可以这样访问它:

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

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

【讨论】:

    【解决方案2】:
    NSDictionary *resultDictionary = [jsonArray objectAtIndex: 0];
    NSArray *resultArray = [resultDictionary objectForKey:@"result"];
    
    for (NSString *item in resultArray)
    {
        NSLog (@"item: %@",item);
    }
    

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

    【讨论】:

      【解决方案3】:

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

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

      为了检查 json 节点,你可以将 json 放在这个站点中,所有节点都会正确显示

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-22
        • 2019-11-16
        相关资源
        最近更新 更多