Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change JSON key name Swift?

In my application I have used the same keyname to get data everywhere, now in json response that data is same but in one place the keyname is changed so I want to rename the keyname of the array in my json this is what I am getting searched on stack overflow but unable to find any reliable way please guide me any good way to do it

{"status":"success","msg":"deleted","pro_data":[]}

I want JSON with these keys:

{"status":"success","msg":"deleted","Images":[]}
like image 533
Jhony Avatar asked Oct 19 '25 17:10

Jhony


1 Answers

you can use Codable to create JSON model and in that you can customise you key.

I assume your JSON response ({"status":"success","msg":"deleted","pro_data":[]}) available in Data format.

So, See the following code which are used to create JSON model for your data.

struct WSModel: Codable {
    var status  : String?
    var msg     : Int?
    var Images  : [Any]?

    enum CodingKeys: String, CodingKey {
        case currentPage    = "status"
        case msg            = "msg"
        case Images         = "pro_data"
    }
}

Due to there aren't any data type inside your array I have keep Any type of data.This code is work for when keys in response are "status", "msg", "pro_data".

Try this code and let me know still an issue. I hope this will work for you.

like image 135
ashish Avatar answered Oct 22 '25 06:10

ashish



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!