【问题标题】:Only able to parse SOME of JSON data, syntax issue?只能解析一些 JSON 数据,语法问题?
【发布时间】:2018-11-05 05:44:16
【问题描述】:

我在将一些从牛津词典检索到的 JSON 数据解析到我的应用程序时遇到问题。

我正在将 JSON 响应打印到我的控制台中,这确认我已成功获取所需的数据。

验证是否正在检索 JSON 数据:

当我取回 JSON 数据时,最初有两个主要文件夹。 “results”,里面包含了我需要的信息:词定义等信息

和“元数据”。

我从牛津词典返回的 JSON 格式:

我能够解析“元数据”文件夹中包含的信息,并将其打印到控制台以确认。

验证我能够解析和打印元数据:

但是,我似乎无法解析“结果”文件夹中包含的任何数据,而这正是我真正需要的。定义、语言、单词 ID 等等。 我在这里做错了什么??

解析“结果”数据的问题示例:

另一个尝试解析“结果”的错误示例:

我的 JSON 响应的纯文本副本:

{
    "metadata": {
        "provider": "Oxford University Press"
    },
    "results": [
        {
            "id": "ace",
            "language": "en",
            "lexicalEntries": [
                {
                    "entries": [
                        {
                            "etymologies": [
                                "Middle English (denoting the ‘one’ on dice): via Old French from Latin as ‘unity, a unit’"
                            ],
                            "grammaticalFeatures": [
                                {
                                    "text": "Singular",
                                    "type": "Number"
                                }
                            ],
                            "homographNumber": "100",
                            "senses": [
                                {
                                    "definitions": [
                                        "a playing card with a single spot on it, ranked as the highest card in its suit in most card games"
                                    ],
                                    "domains": [
                                        "Cards"
                                    ],
                                    "examples": [
                                        {
                                            "registers": [
                                                "figurative"
                                            ],
                                            "text": "life had started dealing him aces again"
                                        },
                                        {
                                            "text": "the ace of diamonds"
                                        }
                                    ],
                                    "id": "m_en_gbus0005680.006",
                                    "short_definitions": [
                                        "playing card with single spot on it, ranked as highest card in its suit in most card games"
                                    ]
                                },
                                {
                                    "definitions": [
                                        "a person who excels at a particular sport or other activity"
                                    ],
                                    "domains": [
                                        "Sport"
                                    ],
                                    "examples": [
                                        {
                                            "text": "a motorcycle ace"
                                        }
                                    ],
                                    "id": "m_en_gbus0005680.010",
                                    "registers": [
                                        "informal"
                                    ],
                                    "short_definitions": [
                                        "person who excels at particular sport or other activity"
                                    ],
                                    "subsenses": [
                                        {
                                            "definitions": [
                                                "a pilot who has shot down many enemy aircraft"
                                            ],
                                            "domains": [
                                                "Air Force"
                                            ],
                                            "examples": [
                                                {
                                                    "text": "a Battle of Britain ace"
                                                }
                                            ],
                                            "id": "m_en_gbus0005680.011",
                                            "short_definitions": [
                                                "pilot who has shot down many enemy aircraft"
                                            ]
                                        }
                                    ],
                                    "thesaurusLinks": [
                                        {
                                            "entry_id": "ace",
                                            "sense_id": "t_en_gb0000173.001"
                                        }
                                    ]
                                },
                                {
                                    "definitions": [
                                        "(in tennis and similar games) a service that an opponent is unable to return and thus wins a point"
                                    ],
                                    "domains": [
                                        "Tennis"
                                    ],
                                    "examples": [
                                        {
                                            "text": "Nadal banged down eight aces in the set"
                                        }
                                    ],
                                    "id": "m_en_gbus0005680.013",
                                    "short_definitions": [
                                        "(in tennis and similar games) service that opponent is unable to return and thus wins point"
                                    ],
                                    "subsenses": [
                                        {
                                            "definitions": [
                                                "a hole in one"
                                            ],
                                            "domains": [
                                                "Golf"
                                            ],
                                            "examples": [
                                                {
                                                    "text": "his hole in one at the 15th was Senior's second ace as a professional"
                                                }
                                            ],
                                            "id": "m_en_gbus0005680.014",
                                            "registers": [
                                                "informal"
                                            ],
                                            "short_definitions": [
                                                "hole in one"
                                            ]
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            "etymologies": [
                                "early 21st century: abbreviation of asexual, with alteration of spelling on the model of ace"
                            ],
                            "grammaticalFeatures": [
                                {
                                    "text": "Singular",
                                    "type": "Number"
                                }
                            ],
                            "homographNumber": "200",
                            "senses": [
                                {
                                    "definitions": [
                                        "a person who has no sexual feelings or desires"
                                    ],
                                    "domains": [
                                        "Sex"
                                    ],
                                    "examples": [
                                        {
                                            "text": "both asexual, they have managed to connect with other aces offline"
                                        }
                                    ],
                                    "id": "m_en_gbus1190638.004",
                                    "short_definitions": [
                                        "asexual person"
                                    ]
                                }
                            ]
                        }
                    ],
                    "language": "en",
                    "lexicalCategory": "Noun",
                    "pronunciations": [
                        {
                            "audioFile": "http://audio.oxforddictionaries.com/en/mp3/ace_1_gb_1_abbr.mp3",
                            "dialects": [
                                "British English"
                            ],
                            "phoneticNotation": "IPA",
                            "phoneticSpelling": "eɪs"
                        }
                    ],
                    "text": "ace"
                },
                {
                    "entries": [
                        {
                            "grammaticalFeatures": [
                                {
                                    "text": "Positive",
                                    "type": "Degree"
                                }
                            ],
                            "homographNumber": "101",
                            "senses": [
                                {
                                    "definitions": [
                                        "very good"
                                    ],
                                    "examples": [
                                        {
                                            "text": "Ace! You've done it!"
                                        },
                                        {
                                            "text": "an ace swimmer"
                                        }
                                    ],
                                    "id": "m_en_gbus0005680.016",
                                    "registers": [
                                        "informal"
                                    ],
                                    "short_definitions": [
                                        "very good"
                                    ],
                                    "thesaurusLinks": [
                                        {
                                            "entry_id": "ace",
                                            "sense_id": "t_en_gb0000173.002"
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            "grammaticalFeatures": [
                                {
                                    "text": "Positive",
                                    "type": "Degree"
                                }
                            ],
                            "homographNumber": "201",
                            "senses": [
                                {
                                    "definitions": [
                                        "(of a person) having no sexual feelings or desires; asexual"
                                    ],
                                    "domains": [
                                        "Sex"
                                    ],
                                    "examples": [
                                        {
                                            "text": "I didn't realize that I was ace for a long time"
                                        }
                                    ],
                                    "id": "m_en_gbus1190638.006",
                                    "short_definitions": [
                                        "asexual"
                                    ]
                                }
                            ]
                        }
                    ],
                    "language": "en",
                    "lexicalCategory": "Adjective",
                    "pronunciations": [
                        {
                            "audioFile": "http://audio.oxforddictionaries.com/en/mp3/ace_1_gb_1_abbr.mp3",
                            "dialects": [
                                "British English"
                            ],
                            "phoneticNotation": "IPA",
                            "phoneticSpelling": "eɪs"
                        }
                    ],
                    "text": "ace"
                },
                {
                    "entries": [
                        {
                            "grammaticalFeatures": [
                                {
                                    "text": "Transitive",
                                    "type": "Subcategorization"
                                },
                                {
                                    "text": "Present",
                                    "type": "Tense"
                                }
                            ],
                            "homographNumber": "102",
                            "senses": [
                                {
                                    "definitions": [
                                        "(in tennis and similar games) serve an ace against (an opponent)"
                                    ],
                                    "domains": [
                                        "Tennis"
                                    ],
                                    "examples": [
                                        {
                                            "text": "he can ace opponents with serves of no more than 62 mph"
                                        }
                                    ],
                                    "id": "m_en_gbus0005680.020",
                                    "registers": [
                                        "informal"
                                    ],
                                    "short_definitions": [
                                        "(in tennis and similar games) serve ace against"
                                    ],
                                    "subsenses": [
                                        {
                                            "definitions": [
                                                "score an ace on (a hole) or with (a shot)"
                                            ],
                                            "domains": [
                                                "Golf"
                                            ],
                                            "examples": [
                                                {
                                                    "text": "there was a prize for the first player to ace the hole"
                                                }
                                            ],
                                            "id": "m_en_gbus0005680.026",
                                            "short_definitions": [
                                                "score ace on hole or with"
                                            ]
                                        }
                                    ]
                                },
                                {
                                    "definitions": [
                                        "achieve high marks in (a test or exam)"
                                    ],
                                    "examples": [
                                        {
                                            "text": "I aced my grammar test"
                                        }
                                    ],
                                    "id": "m_en_gbus0005680.028",
                                    "regions": [
                                        "North American"
                                    ],
                                    "registers": [
                                        "informal"
                                    ],
                                    "short_definitions": [
                                        "achieve high marks in"
                                    ],
                                    "subsenses": [
                                        {
                                            "definitions": [
                                                "outdo someone in a competitive situation"
                                            ],
                                            "examples": [
                                                {
                                                    "text": "the magazine won an award, acing out its rivals"
                                                }
                                            ],
                                            "id": "m_en_gbus0005680.029",
                                            "notes": [
                                                {
                                                    "text": "\"ace someone out\"",
                                                    "type": "wordFormNote"
                                                }
                                            ],
                                            "short_definitions": [
                                                "outdo someone in competitive situation"
                                            ]
                                        }
                                    ]
                                }
                            ]
                        }
                    ],
                    "language": "en",
                    "lexicalCategory": "Verb",
                    "pronunciations": [
                        {
                            "audioFile": "http://audio.oxforddictionaries.com/en/mp3/ace_1_gb_1_abbr.mp3",
                            "dialects": [
                                "British English"
                            ],
                            "phoneticNotation": "IPA",
                            "phoneticSpelling": "eɪs"
                        }
                    ],
                    "text": "ace"
                }
            ],
            "type": "headword",
            "word": "ace"
        }
    ]
}

【问题讨论】:

  • 能否请您发布您的 JSON 的明文?图片不完整,不打算重新输入测试。
  • 当然。刚刚添加到帖子中
  • 您需要用整数而不是字符串为数组下标 (json["results"][0]["id"])
  • 谢谢丹!这正是问题所在。呃。

标签: ios json swift swifty-json


【解决方案1】:

这样检索结果:

let results = json["results"][0]["id"]

或者

if let resultObj = json["results"].first {
 let id = resultObj["id"]
}

id 键在结果数组的零索引处的字典中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多