【问题标题】:How to Loop through Json with SwiftyJson如何使用 SwiftyJson 循环 Json
【发布时间】:2019-03-12 15:11:20
【问题描述】:

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

{
"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 主体! 感谢您的帮助

【问题讨论】:

  • 如果您使用的是 Swift 4,我们建议您使用 Codable。如果没有,您必须了解 JSON 的结构。这并不复杂,它只是顶层的字典,它有一个关键区域,其值是字典数组等。如有必要,请逐步执行。那么,你有什么尝试吗?

标签: json swift swifty-json


【解决方案1】:

使您的所有对象符合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 后,您可以根据需要使用 .forEach {} 闭包或 for...in 循环遍历所有 lineswords

【讨论】:

  • 如果你采用Codable你可以删除所有init方法。 Codable 无论如何都不会使用这些。并且仅在您确实需要引用语义时才使用类。
  • 没错,但我不知道这个人的用例(无法从他提供的信息中推断出)——如果更合适的话,他可以使用Structs。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-27
  • 2015-04-06
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
相关资源
最近更新 更多