Y
Y
YeahCode2020-05-01 18:08:21
JSON
YeahCode, 2020-05-01 18:08:21

Value from complex Json structure - Codable + Alamofire?

Guys, tell
me I'm studying Json parsing, working with a multidimensional data array. And I don’t quite understand how to get through to some data correctly, I would be grateful for tips on how to do it right

1. Method

Alamofire.request(serviceName, method: .get, parameters: parameters).validate().responseJSON { response in
            
            let jsonDecoder = JSONDecoder()

            switch(response.result) {
            case .success:
                if let data = response.data {

                do {
                    let json = try jsonDecoder.decode(TopLevel.self, from: data)
                    print("\nJson", json.graph?.shortMedia?.edgeMediaUser?.edges ?? "\nJson nil\n")
                    // При таком пути, получаю массив юзеров (но мне требуются данные которые глубже, в самом этот массиве), но если постучаться к ним, через структуру к примеру требуемого массива, то будет nil. 
                    
                    let taggedUser = try jsonDecoder.decode(EdgeMediaEdge.self, from: data)
                    print("\n-------------1-------------", tUser) // nil
                    print("\n-------------1-------------", tUser.node?.user) // ni
                    
                    let tentacledNode = try jsonDecoder.decode(TeNode.self, from: data)
                    print("\n-------------2-------------", tenNode.user) // ni 
                    
                    let owner = try jsonDecoder.decode(Owner.self, from: data)
                    print("\n-------------3-------------", owner.fullName) // ni
                    completion(json, nil)
                    // .....



// MARK: - TopLevel
struct TopLevel: Codable {
    let graph: Graph?
}

// MARK: - Graph
struct Graph: Codable {
    let shortMedia: ShortMedia?
    
    enum CodingKeys: String, CodingKey {
        case shortMedia = "shortmedia"
    }
}

// MARK: - ShortMedia
struct ShortMedia: Codable {
    let typename, id, shortcode: String?
    let edgeMediaUser: EdgeMediaUser?
    let owner: Owner?
    
    enum CodingKeys: String, CodingKey {
        case typename = "tname"
        case id, shortcode, dimensions
        case edgeMediaUser = "edge_media__user"
        case owner
    }
}


// MARK: - OwnerClass
struct OwnerClass: Codable {
    let id: String?
    let username, fullName: String?
    
    enum CodingKeys: String, CodingKey {
        case id
        case username
        case fullName = "fname"
    }
}

// MARK: - EdgeMediaUser
struct EdgeMediaUser: Codable {
    let edges: [EdgeMediaUserE]?
}

// MARK: - EdgeMediaUserE
struct EdgeMediaUserE: Codable {
    let node: TenNode?
}

// MARK: - TenNode
struct TenNode: Codable {
    let user: OwnerClass?
    let x, y: Double?
}


// MARK: - Owner
struct Owner: Codable {
    let id: String?
    let username: String?
    let fullName: String?
    
    enum CodingKeys: String, CodingKey {
        case id = "id"
        case username
        case fuName = "funame"
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question