我的JSON解析失败了

问题描述 投票:0回答:1

我使用SwiftyJSON,Alamofire_SwiftyJSON和Alamofire库。我正在尝试解析我的回复,但它什么也没有返回(“定义”无法打印)。 JSON不是空的,它得到了响应。

所以,我的代码看起来如下(没有失败,它都编译)

 Alamofire.request(request).responseSwiftyJSON { dataResponse in

                if let JSON = dataResponse.result.value  {
                    print(JSON)

                if let definitions = JSON["results"]["lexicalEntries"]["entries"]["senses"]["definitions"].string {
                    print(definitions)
                    print("Hello")
                    }}
            }

我的响应模型看起来像(这不是整个响应,这正是我想达到的目标:

{
  "results" : [
    {
      "language" : "en",
      "id" : "ace",
      "type" : "headword",
      "lexicalEntries" : [
        {
          "language" : "en",
          "entries" : [
            {
              "etymologies" : [
                "Middle English (denoting the ‘one’ on dice): via Old French from Latin as ‘unity, a unit’"
              ],
              "grammaticalFeatures" : [
                {
                  "type" : "Number",
                  "text" : "Singular"
                }
              ],
              "homographNumber" : "000",
              "senses" : [
                {
                  "definitions" : [
                    "a playing card with a single spot on it, ranked as the highest card in its suit in most card games"

我认为我的问题在于,我应该添加任何括号或任何符号吗?

if let definitions = JSON["results"]["lexicalEntries"]["entries"]["senses"]["definitions"].string
json swift alamofire
1个回答
0
投票

您没有在JSON响应中索引相应的数组,以访问您可以使用的定义数组

JSON['results'][0]['lexicalEntries'][0]['entries'][0]['senses'][0]['definitions']
© www.soinside.com 2019 - 2024. All rights reserved.