Vignesh
Vignesh

Reputation: 644

Swift - "Expected to decode Array<Any> but found a dictionary instead."

I am having a lot of trouble trying to decode this JSON with Swift 4.2:

{
    token = a3253e6ade9b5c7fd99673938bb28b3e;
    user =     {
        "about_me" = Admin;
        address = "";
        "banner_path" = "http://members.sharjahcd.ae/images/000/000/008/1540737652129.jpg?1541043871";
        company = "<null>";
        description = "<null>";
        email = "[email protected]";
        "events_count" = 0;
        "followers_count" = 0;
        "following_count" = 0;
        id = 2;
        "image_path" = "http://members.sharjahcd.ae/images/000/000/009/1540737834886.jpg?1541043896";
        "is_admin" = 1;
        major = "";
        "my_post_count" = 5;
        name = "Admin MemberPortal";
        "non_business_email" = "<null>";
        passedout = "";
        "phone_number" = 000000000;
        photos =         (
            "http://members.sharjahcd.ae/images/000/000/004/IMG_20181029_150700.jpg?1540805844",
            "http://members.sharjahcd.ae/images/000/000/012/20181028_173702.jpg?1541126171",
            "http://members.sharjahcd.ae/images/000/000/011/20181028_173703.jpg?1541126151",
            "http://members.sharjahcd.ae/images/000/000/010/20181028_173705.jpg?1541126144"
        );
        position = "<null>";
        role = Admin;
        stars = 3;
        "total_membership_points" = 0;
    };
}

Here how am trying to decode it

let UserDatas = try JSONDecoder().decode(Token.self, from: data)
  print("response : \(UserDatas)")


struct Token: Decodable {
    let token: String
    let user: [Response]
}

struct Response: Decodable {
    let about_me: String
    let address: String
    let company: String
    let description: String
    let email: String
    let events_count: Int
    let followers_count: Int
    let following_count: Int
    let id = Int()
    let image_path: String
    let is_admin: String
    let major: String
    let my_post_count: Int
    let name: String
    let non_business_email: String
    let passedout: String
    let phone_number: Int
    let photos: [String]
    let position: String
    let role: String
    let stars: Int
    let total_membership_points: Int
}

It doesn't work and got the following error:

typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "user", intValue: nil)], debugDescription: "Expected to decode Array but found a dictionary instead.", underlyingError: nil))

How do I make a struct that will decode this JSON?

Upvotes: 3

Views: 9380

Answers (2)

Ugo Arangino
Ugo Arangino

Reputation: 2958

You may also want to use CodingKey's.

struct Landmark: Codable {
    var name: String
    var foundingYear: Int
    var location: Coordinate
    var vantagePoints: [Coordinate]

    enum CodingKeys: String, CodingKey {
        case name = "title"
        case foundingYear = "founding_date"

        case location
        case vantagePoints
    }
}

See: Encoding and Decoding Custom Types

Upvotes: 2

rodskagg
rodskagg

Reputation: 3937

In your JSON, the user property isn't an array, but you have it declared as an Array ([Response]) in your code. Change your code to:

struct Token: Decodable {
    let token: String
    let user: Response
}

Also, if you have any properties declared in your code that's null or missing from your JSON, you should make them optional.

In your JSON, company seems to be null, so change

let company: String

to

let company: String?

And do the same for other properties that might be null or missing.

Upvotes: 3

Related Questions