首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >IOS/Swift/ JSON :用swiftyJSON解析嵌套的JSON

IOS/Swift/ JSON :用swiftyJSON解析嵌套的JSON
EN

Stack Overflow用户
提问于 2018-06-02 06:54:07
回答 1查看 388关注 0票数 0

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

回答 1

Stack Overflow用户

发布于 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"])
                    }
                 }
             }
      }
   }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50651887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档