简体   繁体   English

Swift4 Json Parse Alamofire设置结构

[英]Swift4 Json Parse Alamofire Set Struct

I have a jsonArray data like that: 我有一个像这样的jsonArray数据:

[
     {
       "data1":"data1",
       "data2":"data2",
       "data3":"data3",

       },
       {
       "data1":"data1",
       "data2":"data2",
       "data3":[
              {
              "data1":"data1",
              "daya2":"data2"
              }]          
   }
]

Data3 may be a string or String array like above. Data3可以是字符串或类似上面的String数组。 My struct is: 我的结构是:

  struct Datas: Decodable{
        let data1: String?
        let data2: String?
        let data3: String?
      }

There is no problem when data3 is string, but I get an error when data3 is array "Expected to decode String but found an array instead.", underlyingError: nil) ) 当data3为字符串时没有问题,但是当data3为数组时出现错误“预期要解码String而是找到一个数组。”,underlyingError:nil)

I cant solve that. 我无法解决。 What is the type of Data3? 什么是Data3类型? Any object does not work. 任何对象都不起作用。

Rather than AnyObject you'll probably want to use the Any type instead. 您可能要使用Any类型而不是AnyObject Then use an if let to cast to either and array or string. 然后使用if let强制转换为and数组或字符串。 You could also consider use the very helpful SwiftyJSON library to help with processing JSON objects. 您还可以考虑使用非常有用的SwiftyJSON库来帮助处理JSON对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM