gpt4 book ai didi

json - iOS/Swift/JSON : Parse nested JSON with swiftyJSON

转载 作者:行者123 更新时间:2023-11-30 11:26:54 25 4
gpt4 key购买 nike

我在解析以下嵌套 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
}
}
}

最佳答案

要在tone_name打印愤怒

    if let result = response.result.value as? [String:Any] {
if let tone = result["document_tone"] as? [String:Any] {

if let cat = tone["tone_categories"] as? [[string:Any]]{
if let dic = cat[0] as? [string:Any]{

if let tones = dic ["tones"] as? [[String:Any]] {

if let toneDic = tones[0] as? [string:Any]{
print(toneDic["tone_name"]) //print Anger
}
}
}
}
}
}

关于json - iOS/Swift/JSON : Parse nested JSON with swiftyJSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50651887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com