最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

json - Response key value position changing in iOS swift - Stack Overflow

programmeradmin3浏览0评论

I am using Alamofire for API call

Actual response, when I call api in browser or POSTMAN:

{
  "code": 200,
  "message": "Listing found.",
  "data": [
    {
      "_id": "67c5596998c649dfa0d45a48",
      "tableData": [
        {
          "Name": "Mose Boyle",
          "Email": "[email protected]",
          "Phone": "309.534.1394"
        }
      ]
    }
  ]
}

But when I call in my iOS response this happens

{
  "code": 200,
  "message": "Listing found.",
  "data": [
    {
      "_id": "67c5596998c649dfa0d45a48",
      "tableData": [
        {
          "Email": "[email protected]",
          "Phone": "309.534.1394",
          "Name": "Mose Boyle"
        }
      ]
    }
  ]
}

Position randomly changes.

Keys are dynamic so I can't create the model for that.

I am using Alamofire for API call

Actual response, when I call api in browser or POSTMAN:

{
  "code": 200,
  "message": "Listing found.",
  "data": [
    {
      "_id": "67c5596998c649dfa0d45a48",
      "tableData": [
        {
          "Name": "Mose Boyle",
          "Email": "[email protected]",
          "Phone": "309.534.1394"
        }
      ]
    }
  ]
}

But when I call in my iOS response this happens

{
  "code": 200,
  "message": "Listing found.",
  "data": [
    {
      "_id": "67c5596998c649dfa0d45a48",
      "tableData": [
        {
          "Email": "[email protected]",
          "Phone": "309.534.1394",
          "Name": "Mose Boyle"
        }
      ]
    }
  ]
}

Position randomly changes.

Keys are dynamic so I can't create the model for that.

Share Improve this question edited Mar 25 at 10:28 Larme 26.1k6 gold badges58 silver badges87 bronze badges Recognized by Mobile Development Collective asked Mar 25 at 10:09 Keval dattaniKeval dattani 11 bronze badge 3
  • It's JSON "Dictionary". Let me ask you this, in Swift: let dict 1 = ["A": 1, "B": 2] is the same as let dict2 = ["B": 2, "A": 1] or are they different? Dictionary is a basic concept that you must master at the beginning of your coding journey. – Larme Commented Mar 25 at 10:29
  • "Keys are dynamic so I can't create the model for that.", what do you mean with this more precisely, is it only the order that is dynamic? And what is your actual question? – Joakim Danielson Commented Mar 25 at 11:55
  • Dictionaries are unordered – lorem ipsum Commented Mar 25 at 11:58
Add a comment  | 

1 Answer 1

Reset to default 0

Because it is... what it is, according to the JSON definition:

An object is an unordered set of name/value pairs. An object begins with {left brace and ends with }right brace. Each name is followed by :colon and the name/value pairs are separated by ,comma.

So, its order doesn't matter, you can conform to Codable to parse it, like this:

struct TableData: Codable {
    let id: String
    let tableData: [Person]
    
    enum CodingKeys: String, CodingKey {
        case id = "_id"
        case tableData
    }
}

struct Person: Codable {
    let name: String
    let email: String
    let phone: String
    
    enum CodingKeys: String, CodingKey {
        case name = "Name"
        case email = "Email"
        case phone = "Phone"
    }
}
发布评论

评论列表(0)

  1. 暂无评论