0

I have a jsonArray data like that:

[
     {
       "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. 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))

I cant solve that. What is the type of Data3? Any object does not work.

1

1 Answer 1

0

Rather than AnyObject you'll probably want to use the Any type instead. Then use an if let to cast to either and array or string. You could also consider use the very helpful SwiftyJSON library to help with processing JSON objects.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.