As If Prince
As If Prince

Reputation: 297

How to map nested JSON response?

I am getting some JSON response from the server that are in nested Dictionary order. The response i get looks like this:

data =     {
"cart_count" = "5.8";
"del_fee_popup" = 1;
"delievery_fee" = 26;
"delievery_fee_message" = "Free delivery on order more than 500 tk.";

"delievery_fee_types" =         (
                {
        "delievery_fee" = 26;
        "delievery_fee_type" = 1;
        "from_time" = 7;
        "from_time_str" = "7 AM";
        message = "Estimated delivery time 1.5~4hrs after accepted the order";
        title = "Regular Delivery";
        "to_time" = 22;
        "to_time_str" = "10 PM";
    },
                {
        "delievery_fee" = 80;
        "delievery_fee_type" = 2;
        "from_time" = 10;
        "from_time_str" = "10 AM";
        message = "Estimated delivery time 1~1.5hr after accept the order";
        title = "Urgent Delivery";
        "to_time" = 0;
        "to_time_str" = "12 AM";
    }
);
discount = 0;
total = "31.8";

I am able to map the "data" from the response like this:

 mutating func deserilize(values: Dictionary<String, Any>?) {
    Data = (data as! [String : Any])
    deliveryFee = Data!["delievery_fee"] as! Double
    total = Data!["total"] as! Double
    discount = Data!["discount"] as! Double
    cartCount = Data!["cart_count"] as! Double
    del_fee_popup = Data!["del_fee_popup"] as! Bool
}

I need some suggestion to map the "delievery_fee_types" from the JSON response.

Upvotes: 0

Views: 121

Answers (1)

Abv
Abv

Reputation: 424

create a new swift file:

import UIKit

struct JData: Decodable {
    let cart_count: Float?
    let del_fee_popup: Int?
    let delievery_fee: Int?
    let delievery_fee_message: String?
    let discount: Int?
    let total: Float?
    let delivery_fee_types: [Delivery_fee_types?]
    
}

struct Delivery_fee_types: Decodable {
    let delievery_fee: Int?
    let delievery_fee_type: Int?
    let from_time: Int?
    let from_time_str: String?
    let message: String?
    let title: String?
    let to_time: Int?
    let to_time_str: String?
}

if you load your Json locally, create an empty file, name it data.json, paste the json below in the file:

{"cart_count" : "5.8", "del_fee_popup" : 1, "delievery_fee" : 26, "delievery_fee_message" : "Free delivery on order more than 500 tk.","delievery_fee_types" :[ { "delievery_fee" : 26, "delievery_fee_type" : 1, "from_time" : 7, "from_time_str" : "7 AM", "message" : "Estimated delivery time 1.5~4hrs after accepted the order", "title" : "Regular Delivery", "to_time" : 22, "to_time_str" : "10 PM" }, { "delievery_fee" : 80, "delievery_fee_type" : 2, "from_time" : 10, "from_time_str" : "10 AM", "message" : "Estimated delivery time 1~1.5hr after accept the order", "title" : "Urgent Delivery", "to_time" : 0, "to_time_str" : "12 AM" } ], "discount" : 0, "total" : "31.8"}

create a new swift file and add:

import UIKit

class JsonClass: NSObject {

    class func readLocalFile(forName name: String) -> Data? {
        do {
            if let bundlePath = Bundle.main.path(forResource: name,
                                             ofType: "json"),
                let jsonData = try String(contentsOfFile: bundlePath).data(using: .utf8) {
                return jsonData
            }
        } catch {
            print(error)
        }
        return nil
    }

    class func parse(jsonData: Data) -> JData? {
        do {
            let decodedData = try JSONDecoder().decode(JData.self, from: jsonData)
            return decodedData
        } catch {
            print("decode error: ",error)
        }
        return nil
    }
}

where you want to have your data use:

 if let localData = JsonClass.readLocalFile(forName: "data") {
        if let decodedJson = JsonClass.parse(jsonData: localData) {
            print(decodedJson as Any)
            // example print(decodedJson.cart_count as any)
        }
  }

Upvotes: 2

Related Questions