如何检查 swiftyJSON 中的空 JSONArray

How to check an empty JSONArray in swiftyJSON

我有一个 JSON,它有一个 JSON 数组作为其中一个 json 的值。这是它的例子。

[
  {
    "id": 1,
    "symptoms" : [{\"key\":\"sample1\",\"value\":5},{\"key\":\"sample2\",\"value\":5}]
  },
  {
    "id": 2,
    "symptoms" : [{\"key\":\"sample3\",\"value\":1}]
  },
  { "id": 3,
    "symptoms" : []
  },
  {
    "id": 4,
    "symptoms": [{\"key\":\"sample4\",\"value\":1}]
  }
]

所以我正在做的是解析内部 JSON 并将其放入字符串数组中。但是每当我查找 symptoms 时,它都会跳过空的 JSON 数组。因此,每当我打印字符串数组时,它都会像这样(顶部有给定的示例)["sample1", "sample2", "sample3", "sample4"]。但我想做的是在 JSON 数组为空时将 "" 附加到字符串数组,所以它应该像这样 ["sample1", "sample2", "sample3", "", "sample4"]。任何人都可以帮助我吗?这是我的代码

var arrayHolder: [String] = []
var idHolder: [Int] = []
for item in swiftyJSON.arrayValue {
    idHolder.append(item["id"].intValue)

    //for the inner JSON
    let innerJSON = JSON(data: item["symptoms"].dataUsingEncoding(NSUTF8StringEncoding)!)
    for symptoms in innerJSON.arrayValue {
        arrayHolder.append(symptoms["key"].stringValue)
    }
}
print(idHolder) // [1,2,3,4]
print(arrayHolder) // ["sample1","sample2","sample3","sample4"]

只需检查 innerJSON 是否为空:

for item in swiftyJSON.arrayValue {
    idHolder.append(item["id"].intValue)

    //for the inner JSON
    let innerJSON = item["symptoms"].arrayValue // non need to create a new JSON object

    if innerJSON.isEmpty {
        arrayHolder.append("")
    } else {
       for symptoms in innerJSON {
           arrayHolder.append(symptoms["key"].stringValue)
       }
    }
}