如何使用SwiftyJson遍历Json

问题描述 投票:-1回答:1

如何使用SwiftyJson在下面的json文件中使用键“text”提取所有值?这是来自Microsoft认知服务文本识别的响应。

{
"language": "de",
"textAngle": 0,
"orientation": "Up",
"regions": [
    {
        "boundingBox": "353,597,1926,1277",
        "lines": [
            {
                "boundingBox": "1091,597,410,93",
                "words": [
                    {
                        "boundingBox": "1091,604,106,84",
                        "text": "Ka"
                    },
                    {
                        "boundingBox": "1210,597,291,93",
                        "text": "uflond"
                    }
                ]
            },
            {
                "boundingBox": "1122,861,358,89",
                "words": [
                    {
                        "boundingBox": "1122,866,174,84",
                        "text": "Kauf"
                    },
                    {
                        "boundingBox": "1314,865,21,85",
                        "text": "I"
                    },
                    {
                        "boundingBox": "1348,861,132,87",
                        "text": "and"
                    }
                ]
            },
            {
                "boundingBox": "948,982,649,81",
                "words": [
                    {
                        "boundingBox": "948,986,398,77",
                        "text": "Schwabenp"
                    },
                    {
                        "boundingBox": "1359,984,19,74",
                        "text": "I"
                    },
                    {
                        "boundingBox": "1393,986,131,69",
                        "text": "atz"
                    },
                    {
                        "boundingBox": "1546,982,51,74",
                        "text": "LI"
                    }
                ]
            },
            {
                "boundingBox": "944,1088,665,80",
                "words": [
                    {
                        "boundingBox": "944,1089,220,79",
                        "text": "70563"
                    },
                    {
                        "boundingBox": "1210,1088,399,75",
                        "text": "Stutt

json文件要大得多,并且有很多值具有不同深度的关键“文本”,我不知道如何循环整个Json体!谢谢你的帮助

json swift swifty-json
1个回答
0
投票

将所有对象整合到Codable,然后在实例化实例后,根据需要进行挖掘,例如使用TextDto包装类:

class TextDto: Codable {
    let language: String
    let textAngle: Int
    let orientation: String
    let regions: [Region]

    init(language: String, textAngle: Int, orientation: String, regions: [Region]) {
        self.language = language
        self.textAngle = textAngle
        self.orientation = orientation
        self.regions = regions
    }
}

class Region: Codable {
    let boundingBox: String
    let lines: [Line]

    init(boundingBox: String, lines: [Line]) {
        self.boundingBox = boundingBox
        self.lines = lines
    }
}

class Line: Codable {
    let boundingBox: String
    let words: [Word]

    init(boundingBox: String, words: [Word]) {
        self.boundingBox = boundingBox
        self.words = words
    }
}

class Word: Codable {
    let boundingBox, text: String

    init(boundingBox: String, text: String) {
        self.boundingBox = boundingBox
        self.text = text
    }
}

一旦你从api获得了TextDto,你可以用你认为合适的lines闭合或words循环遍历所有.forEach {}for...in

© www.soinside.com 2019 - 2024. All rights reserved.