Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何从数组中的字典中获取值?_Ios_Parsing - Fatal编程技术网

Ios 如何从数组中的字典中获取值?

Ios 如何从数组中的字典中获取值?,ios,parsing,Ios,Parsing,我想在一些字符串中存储一条消息——“hi this is John KL”,说明如何解析下面的示例 [ { "message": "hi this is John KL" } ] 斯威夫特: 目标C: -(void)readJSON{ n错误*结果; NSURL*url=[[NSBundle mainBundle]URLForResource:@“样本” 扩展名为:@“JSON”]; NSData*data=[NSData dataWithContentsOfU

我想在一些字符串中存储一条消息——“hi this is John KL”,说明如何解析下面的示例

[
    {
        "message": "hi this is John KL"
    }
]
斯威夫特: 目标C:
-(void)readJSON{
n错误*结果;
NSURL*url=[[NSBundle mainBundle]URLForResource:@“样本”
扩展名为:@“JSON”];
NSData*data=[NSData dataWithContentsOfURL:url
选项:0
错误:&结果];
如果(结果!=零){
NSLog(@“读取文件时出错:%@”,结果);
回来
}
NSArray*数组=[NSJSONSerialization JSONObjectWithData:数据选项:0错误:&结果];
如果(结果!=零){
NSLog(@“错误转换JSON:%@”,结果);
回来
}
否则{
NSLog(@“\nJSON data=\n%@”,数组);
if(array.count<1){
NSLog(@“数组中的元素不足”);
回来
}
NSString*消息=数组[0][@“消息”];
如果(消息==nil){
NSLog(@“无法获取消息”);
}否则{
NSLog(@“Message=\“%@\”,Message);
}
}
}
上面的Objective-C代码没有测试以确保从JSON文件读取的对象是正确的类型。如果它不是包含具有字符串键和字符串值的字典的数组,它将崩溃。对于生产应用程序,您需要添加代码以键入检查数据。

Swift
将json数组分配给变量类型
[String:String]
[String:Any]
字典数组
[String:Any]
是最常用的词典,但根据您的数据,它与
[String:String]

if let array = [
                 {
                     “message” : “hi this is John KL”
                 }
               ] as [Any]
现在,使用索引值从数组中获取json/字典,然后使用json键从json中获取字符串

if let dictionary = array.first as? [String : Any] {
   if let stringMessage = dictionary["message"] as? String {
       print("stringMessage - \(stringMessage)")
   }
}
目标-C

NSArray * array = [
                     {
                         “message” : “hi this is John KL”
                     }
                   ];


NSDictionary * dictionary = (NSDictionary *)[array objectAtIndex: 0];

NSString * stringMessage = (NSString *)[dictionary valueForKey: "message"];

NSLog(@"stringMessage - %@",stringMessage);

你可以试试这个答案

NSArray *result = [json objectForKey:@"result"];
        for(NSString *currenObject in result){
            NSLog(@"%@",currenObject);
           NSString *currentValue = [currenObject valueForKey:@"message"];
}

  NSLog(@"%@",currentValue);

作为一个初学者,展示你写了什么来解决问题。顺便说一下,这是您的代码,让dictData=yourayOfdict[index]作为字典。让valueObject=dictData[“消息”]。要解析JSON,请查看此链接,使用JSONSerialization获取object。这个问题是专门针对Objective-C提出的tag@AdityaSrivastava嗯,不,不是。我看了看,回答时没有特定语言的标签。OP在我回答后添加了这个标签。很好地提供了Swift和Objective-C代码。(投票)您可能希望在数组中添加类型说明符,这样就不必强制转换它。它还可以使用错误检查。@Duncac谢谢,欢迎您编辑和改进此答案!我的Objective-C最近生锈了。如果我想使用类型化数组/字典语法,我必须查找它。我在回答中发布了一个完整的Objective-C方法,包括JSON解码和一个强类型NSArray。
NSArray * array = [
                     {
                         “message” : “hi this is John KL”
                     }
                   ];


NSDictionary * dictionary = (NSDictionary *)[array objectAtIndex: 0];

NSString * stringMessage = (NSString *)[dictionary valueForKey: "message"];

NSLog(@"stringMessage - %@",stringMessage);
NSArray *result = [json objectForKey:@"result"];
        for(NSString *currenObject in result){
            NSLog(@"%@",currenObject);
           NSString *currentValue = [currenObject valueForKey:@"message"];
}

  NSLog(@"%@",currentValue);