我在解析以下嵌套的JSON时遇到了问题。我可以获得第一个级别,但不能获得后续级别。有谁能建议正确的语法来获得“愤怒”?提前感谢您的任何建议。
JSON:
{
"document_tone" = {
"tone_categories" = (
{
"category_id" = "emotion_tone";
"category_name" = "Emotion Tone";
tones = (
{
score = "0.218727";
"tone_id" = anger;
"tone_name" = Anger;
},
);
},
);
}
代码:
if let result = response.result.value as? [String:Any] {
if let tone = result["document_tone"] as? [String:Any] {
print(tone) //This prints ok
if let cat = tone["tone_categories"] as? String {
print("here is%@",cat)//prints as null
}
}
}
发布于 2018-06-02 07:32:13
你可以试试
if let result = response.result.value as? [String:Any] {
if let tone = result["document_tone"] as? [String:Any] {
print(tone)
if let cat = tone["tone_categories"] as? [[String:Any]] {
if let dic = cat[0] as? [String:Any] {
if let catId = dic["category_id"] as? String {
print("catId is %@",catId)
}
if let catName = dic["category_name"] as? String {
print("catName is %@",catName )
}
if let alltones = dic["tones"] as? [[String:Any]] {
print("alltones is %@",alltones)
if let innerTone = alltones[0] as? [String:Any] {
print(innerTone["tone_name"])
print(innerTone["tone_id"])
}
}
}
}
}
}
https://stackoverflow.com/questions/50651887
复制相似问题