我使用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响应中索引相应的数组,以访问您可以使用的定义数组
JSON['results'][0]['lexicalEntries'][0]['entries'][0]['senses'][0]['definitions']