json - alamofire 解析嵌套的 json

标签 json swift xcode alamofire

我是 swift 的新手,第一次在 swift 中使用 alamofire 进行联网,我遇到了几个问题,我无法从我的 API 解析嵌套的 JSON,已经 3 天了,我仍然弄清楚是什么这个问题有问题,这里是示例代码

应用程序接口(interface):

    {
  "id" : "7fc8581a0373715c62750ba2c472d5d3",
  "status" : 1,
  "progress" : [
    {
      "state" : "3",
      "airline_code" : "SRI",
      "data" : {
        "go" : [
          {
            "additional" : {
              "numstop" : 0
            },
            "flights" : [
              {
                "flight_num" : "SJ231",
                "depart_port" : "JOG",
                "depart_timezone" : "Asia\/Jakarta",
                "arrive_timezone" : "Asia\/Jakarta",
                "arrive_city" : "Jakarta, Cengkareng",
                "extended_attribute" : {
                  "bus" : null,
                  "pro" : null,
                  "eco" : {
                    "id" : "6155a71e6fe687b54a97cba4de3091af",
                    "class_letter" : "g",
                    "seat_avail" : "2"
                  }
                },
                "depart_datetime" : "2018-05-05 11:00",
                "id" : "bebbdb13270a448ffa55ed2e928e2d60",
                "depart_city" : "Yogyakarta",
                "arrive_datetime" : "2018-05-05 12:10",
                "flight_duration" : "01:10",
                "arrive_time" : "12:10",
                "depart_time" : "11:00",
                "arrive_port" : "CGK"
              }
            ],
            "id" : "0d82983e6cbcbabd9dfea57c8622253a",
            "fares" : {
              "bus" : null,
              "pro" : null,
              "eco" : {
                "id" : "d06d8c19a5ad77089df538b8b54e5ac9",
                "by_ages" : {
                  "child" : null,
                  "infant" : null,
                  "adult" : {
                    "basic" : 330000,
                    "fuel" : 0,
                    "discount" : 0,
                    "adm" : 50000,
                    "iwjr" : 5000,
                    "surcharge" : 0,
                    "total" : 418000,
                    "tax" : 33000
                  }
                }
              }
            }
          },
          {
            "additional" : {
              "numstop" : 0
            },
            "flights" : [
              {
                "flight_num" : "SJ235",
                "depart_port" : "JOG",
                "depart_timezone" : "Asia\/Jakarta",
                "arrive_timezone" : "Asia\/Jakarta",
                "arrive_city" : "Jakarta, Cengkareng",
                "extended_attribute" : {
                  "bus" : null,
                  "pro" : null,
                  "eco" : {
                    "id" : "d10c02b1fce3ef00d340faacfeebf155",
                    "class_letter" : "v",
                    "seat_avail" : "9"
                  }
                },
                "depart_datetime" : "2018-05-05 17:45",
                "id" : "ad185a29a9e6b302aa6d1b86e933f029",
                "depart_city" : "Yogyakarta",
                "arrive_datetime" : "2018-05-05 18:55",
                "flight_duration" : "01:10",
                "arrive_time" : "18:55",
                "depart_time" : "17:45",
                "arrive_port" : "CGK"
              }
            ],
            "id" : "8dd63fa4f7256a2ac00352519ae787d6",
            "fares" : {
              "bus" : null,
              "pro" : null,
              "eco" : {
                "id" : "436ab688a1e4512d41c95e55e2fdcdea",
                "by_ages" : {
                  "child" : null,
                  "infant" : null,
                  "adult" : {
                    "basic" : 360000,
                    "fuel" : 0,
                    "discount" : 0,
                    "adm" : 50000,
                    "iwjr" : 5000,
                    "surcharge" : 0,
                    "total" : 451000,
                    "tax" : 36000
                  }
                }
              }
            }
          },
          {
            "additional" : {
              "numstop" : 0
            },
            "flights" : [
              {
                "flight_num" : "IN279",
                "depart_port" : "JOG",
                "depart_timezone" : "Asia\/Jakarta",
                "arrive_timezone" : "Asia\/Jakarta",
                "arrive_city" : "Jakarta, Cengkareng",
                "extended_attribute" : {
                  "bus" : {
                    "id" : "27bca993eb75f1ca3275f46b8229624e",
                    "class_letter" : "i",
                    "seat_avail" : "2"
                  },
                  "pro" : null,
                  "eco" : {
                    "id" : "933c24d214b8fd55d4237bf1e6eb5f6b",
                    "class_letter" : "g",
                    "seat_avail" : "3"
                  }
                },
                "depart_datetime" : "2018-05-05 19:20",
                "id" : "a0c3bd5f6194975cefae8240abef8754",
                "depart_city" : "Yogyakarta",
                "arrive_datetime" : "2018-05-05 20:30",
                "flight_duration" : "01:10",
                "arrive_time" : "20:30",
                "depart_time" : "19:20",
                "arrive_port" : "CGK"
              }
            ],
            "id" : "614836006a92c2deac59eeaef926cf1b",
            "fares" : {
              "bus" : {
                "id" : "865493da8066ac0c677cecfbb88e181b",
                "by_ages" : {
                  "child" : null,
                  "infant" : null,
                  "adult" : {
                    "basic" : 988000,
                    "fuel" : 0,
                    "discount" : 0,
                    "adm" : 50000,
                    "iwjr" : 5000,
                    "surcharge" : 0,
                    "total" : 1141800,
                    "tax" : 98800
                  }
                }
              },
              "pro" : null,
              "eco" : {
                "id" : "74802ef5e6bd5b93c016fcd98aa63925",
                "by_ages" : {
                  "child" : null,
                  "infant" : null,
                  "adult" : {
                    "basic" : 330000,
                    "fuel" : 0,
                    "discount" : 0,
                    "adm" : 50000,
                    "iwjr" : 5000,
                    "surcharge" : 0,
                    "total" : 418000,
                    "tax" : 33000
                  }
                }
              }
            }
          }
        ]
      },
      "code" : null,
      "message" : null,
      "ctr" : "1",
      "rate" : 100
    }
  ]
}

这里是我解析它的代码:

if let json = response.result.value {
let JSONData_child = JSON(json)
let progress = JSONData_child["progress"]
print(progess)
//heres the issue
let name = progress["data"][0]["go"]["flights"].string
if name != nil {
   print(name!)
}
//
     }

它可以访问嵌套的“进度”,但我似乎无法访问嵌套的 JSON 的“数据”

我想要的只是从 JSON 访问“航类”数据,

我怎样才能做到这一点?

最佳答案

In the JSON response, date can be bundled in complex structure of array and dictionary:

**Array:** (Comes in the bracket `[]`)

**Dictionary:** (Comes in the bracket `{}`)

数组:

数组有不同的属性来访问对象。喜欢:

要获取特定索引处的对象,您可以使用[0]、[1]、[2]...等等。还可以使用 first, last

访问特定索引

例如:

let arr = [Any]()

arr.first // will give value at index 0
arr[0] // will give value of index 0
arr[1] // will give value of index 1
arr[2] // will give value of index 2
arr.last // will give value of last index

字典:

字典有键值对。您可以使用 dict["key"] 访问值。

例如:

let dict = [String: Any]()
let value = dict["key"] // It will give the value for particular `key`

In your case:

如果确定json不会变化,就可以直接访问数据。作为:

let name = progress[0]["data"]["go"][0]["flights"][0]

它会给出字典:

    {
        "flight_num" : "SJ231",
        "depart_port" : "JOG",
        "depart_timezone" : "Asia\/Jakarta",
        "arrive_timezone" : "Asia\/Jakarta",
        "arrive_city" : "Jakarta, Cengkareng",
        "extended_attribute" : {
            "bus" : null,
            "pro" : null,
            "eco" : {
                "id" : "6155a71e6fe687b54a97cba4de3091af",
                "class_letter" : "g",
                "seat_avail" : "2"
            }
        },
        "depart_datetime" : "2018-05-05 11:00",
        "id" : "bebbdb13270a448ffa55ed2e928e2d60",
        "depart_city" : "Yogyakarta",
        "arrive_datetime" : "2018-05-05 12:10",
        "flight_duration" : "01:10",
        "arrive_time" : "12:10",
        "depart_time" : "11:00",
        "arrive_port" : "CGK"
    }

关于json - alamofire 解析嵌套的 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49746605/

相关文章:

php - 如何动态地将选项添加到使用从 php 脚本检索的 json 数据构建的选择列表?

ios - 没有互联网 iOS swift 无法接收本地通知

json - SwiftyJSON - 无法解析 Json

ios - 在没有预处理器宏的情况下,有没有办法在 Xcode 项目的项目级别定义实用的方案特定标志

ios - 旋转和动画 ImageView 没有任何失真?

ios - 设置使用 FaSTLane 的 Xcode 配置

c++ - 设置 Xcode 和 FLTK

ios - 如何知道 segue 是推送还是模态

c# - JSON 对象显示 null

json - 使用 http.Post 将 json 数据发布到远程服务器