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

时间:2018-06-01 22:54:07

标签: json swift swifty-json

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

2 个答案:

答案 0 :(得分:0)

你可以尝试

UIView.animate(withDuration: 1.0, delay: 0.0, options: [.allowUserInteraction], animations: {
            self.hexagon.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 2)
            self.tileB.center.x += self.tileB.bounds.width
            self.tileB.center.y += self.tileB.bounds.height
            self.tileF.center.x -= self.tileF.bounds.width
            self.tileF.center.y += self.tileF.bounds.height
            self.tileH.center.x -= self.tileH.bounds.width
            self.tileH.center.y -= self.tileH.bounds.height
            self.tileD.center.x += self.tileD.bounds.width
            self.tileD.center.y -= self.tileD.bounds.height
        }, completion: nil)

答案 1 :(得分:0)

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
                        }
                    }
                }
            }
        }
    }