【问题标题】:IOS/Swift/SwiftyJSON: Parse Nested JSON from IBM Watson/Bluemix APIIOS/Swift/SwiftyJSON:从 IBM Watson/Bluemix API 解析嵌套 JSON
【发布时间】:2018-11-10 22:37:09
【问题描述】:

我从 IBM Watson 的音调分析器 API 接收到一些 JSON,格式如下所示,用于一段文本。我想做的是在一个对象中捕获 JSON,该对象具有主要色调的属性和值,例如。愤怒:.218 厌恶:2.20 等,用于分析的每个文本部分。当我分析更多文本时,我想将它们添加到这些对象的数组中。

在这个阶段,我只想使用 SwiftyJSON 对 api 做一些事情,但对语法感到困惑:

以下代码,只是打印为 nil:

 let anger = JSON(value)["results"][0]["anger"].array?.map { json in
                    json["anger"].stringValue
                                    }
                print (anger)

不胜感激有关如何实际抓住音调及其价值的指导......在此先感谢您的任何建议。

JSON 看起来像这样:

 {
        "document_tone" =     {
            "tone_categories" =         (
                            {
                    "category_id" = "emotion_tone";
                    "category_name" = "Emotion Tone";
                    tones =                 (
                                            {
                            score = "0.218727";
                            "tone_id" = anger;
                            "tone_name" = Anger;
                        },
                                            {
                            score = "0.210102";
                            "tone_id" = disgust;
                            "tone_name" = Disgust;
                        },
                                            {
                            score = "0.060026";
                            "tone_id" = fear;
                            "tone_name" = Fear;
                        },
                                            {
                            score = "0.076444";
                            "tone_id" = joy;
                            "tone_name" = Joy;
                        },
                                            {
                            score = "0.176849";
                            "tone_id" = sadness;
                            "tone_name" = Sadness;
                        }
                    );
                },
                            {
                    "category_id" = "language_tone";
                    "category_name" = "Language Tone";
                    tones =                 (
                                            {
                            score = 0;
                            "tone_id" = analytical;
                            "tone_name" = Analytical;
                        },
                                            {
                            score = 0;
                            "tone_id" = confident;
                            "tone_name" = Confident;
                        },
                                            {
                            score = 0;
                            "tone_id" = tentative;
                            "tone_name" = Tentative;
                        }
                    );
                },
                            {
                    "category_id" = "social_tone";
                    "category_name" = "Social Tone";
                    tones =                 (
                                            {
                            score = "0.02278";
                            "tone_id" = "openness_big5";
                            "tone_name" = Openness;
                        },
                                            {
                            score = "0.340597";
                            "tone_id" = "conscientiousness_big5";
                            "tone_name" = Conscientiousness;
                        },
                                            {
                            score = "0.541852";
                            "tone_id" = "extraversion_big5";
                            "tone_name" = Extraversion;
                        },
                                            {
                            score = "0.545246";
                            "tone_id" = "agreeableness_big5";
                            "tone_name" = Agreeableness;
                        },
                                            {
                            score = "0.743194";
                            "tone_id" = "emotional_range_big5";
                            "tone_name" = "Emotional Range";
                        }
                    );
                }
            );
        };
    }

【问题讨论】:

    标签: ios json ibm-cloud swifty-json tone-analyzer


    【解决方案1】:

    您可以使用 swift 4 和 URLSession 中的 Codable 结构快速轻松地完成此操作。

    这里是您的问题的纯粹快速解决方案。结构属性映射到您的 json 树,您可能需要更改结构以匹配您收到的 json。您在上面发布的 json 格式无效,否则我可以正确映射它。

    struct Tone: Codable {
        let score: Double
        let tone_id: String
        let tone_name: String
    }
    
    struct Category: Codable {
        let category_id: String
        let category_name: String
        let tones: [Tone]
    }
    
    struct DocumentTone: Codable {
        let toneCategories: [Category]
    }
    
    final class ToneService {
        static func fetchTone(toneUrl: URL, _ completion: @escaping([DocumentTone]) -> Void) {
            URLSession.shared.dataTask(with: toneUrl) { (data, response, error) in
                guard let data = data else { return }
                do {
                    let decoder = JSONDecoder()
                    // If your json returns a single object, use DocumentTone.self instead of [DocumentTone].self, as well change the completion
                    let tone = try decoder.decode([DocumentTone].self, from: data)
                    completion(tone)
                } catch let error {
                    print(error.localizedDescription)
                }
            }.resume()
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多