将JSON对象解析为swift中的字符串数组

时间:2017-05-07 14:10:38

标签: arrays json swift

我有以下Json回复。

{
  "language": "en",
  "textAngle": 0,
  "orientation": "Up",
  "regions": [
    {
      "boundingBox": "96,29,244,474",
      "lines": [
        {
          "boundingBox": "96,29,58,12",
          "words": [
            {
              "boundingBox": "96,29,58,12",
              "text": "SG4207"
            }
          ]
        },
        {
          "boundingBox": "97,64,159,16",
          "words": [
            {
              "boundingBox": "97,65,27,15",
              "text": "Eng"
            },
            {
              "boundingBox": "129,64,34,16",
              "text": "Lieh,"
            },
            {
              "boundingBox": "168,65,37,12",
              "text": "Yuen"
            },
            {
              "boundingBox": "212,65,44,15",
              "text": "Kwan,"
            }
          ]
        },
        {
          "boundingBox": "97,99,243,16",
          "words": [
            {
              "boundingBox": "97,99,52,13",
              "text": "Mobile"
            },
            {
              "boundingBox": "154,99,64,13",
              "text": "Wireless"
            },
            {
              "boundingBox": "223,99,62,13",
              "text": "Solution"
            },
            {
              "boundingBox": "291,99,49,16",
              "text": "Design"
            }
          ]
        },
        {
          "boundingBox": "97,134,117,16",
          "words": [
            {
              "boundingBox": "97,134,44,16",
              "text": "Darryl"
            },
            {
              "boundingBox": "147,134,27,13",
              "text": "and"
            },
            {
              "boundingBox": "179,134,35,16",
              "text": "Ajith"
            }
          ]
        },
        {
          "boundingBox": "96,169,71,16",
          "words": [
            {
              "boundingBox": "96,169,71,16",
              "text": "Weekday"
            }
          ]
        },
        {
          "boundingBox": "97,205,72,16",
          "words": [
            {
              "boundingBox": "97,205,72,16",
              "text": "(Monday)"
            }
          ]
        },
        {
          "boundingBox": "96,241,80,15",
          "words": [
            {
              "boundingBox": "96,244,32,12",
              "text": "gam"
            },
            {
              "boundingBox": "133,248,5,1",
              "text": "-"
            },
            {
              "boundingBox": "143,241,33,15",
              "text": "5pm"
            }
          ]
        },
        {
          "boundingBox": "96,275,72,13",
          "words": [
            {
              "boundingBox": "96,275,72,13",
              "text": "Weekend"
            }
          ]
        },
        {
          "boundingBox": "97,310,77,16",
          "words": [
            {
              "boundingBox": "97,310,77,16",
              "text": "(Saturday)"
            }
          ]
        },
        {
          "boundingBox": "96,347,80,15",
          "words": [
            {
              "boundingBox": "96,350,32,12",
              "text": "gam"
            },
            {
              "boundingBox": "133,354,5,1",
              "text": "-"
            },
            {
              "boundingBox": "143,347,33,15",
              "text": "5pm"
            }
          ]
        },
        {
          "boundingBox": "97,382,41,15",
          "words": [
            {
              "boundingBox": "97,382,41,15",
              "text": "3-Apr"
            }
          ]
        },
        {
          "boundingBox": "97,417,45,15",
          "words": [
            {
              "boundingBox": "97,417,8,12",
              "text": "1"
            },
            {
              "boundingBox": "115,417,27,15",
              "text": "Apr"
            }
          ]
        },
        {
          "boundingBox": "97,452,48,15",
          "words": [
            {
              "boundingBox": "97,452,48,15",
              "text": "ID-Apr"
            }
          ]
        },
        {
          "boundingBox": "96,488,42,15",
          "words": [
            {
              "boundingBox": "96,488,42,15",
              "text": "8-Apr"
            }
          ]
        }
      ]
    }
  ]
}

需要在Swift3中从上面的json创建一个String数组。试过以下

if dictionary["regions"] != nil {

    // Get Regions from the dictionary
    let regions = (dictionary["regions"] as! NSArray).firstObject as? [String:AnyObject]

    // Get lines from the regions dictionary
    let lines = regions!["lines"] as! NSArray


    // Get words from lines
    let inLine = lines.enumerated().map {($0.element as? NSDictionary)?["words"] as! [[String : AnyObject]] }

    // Get text from words
    let extractedText = inLine.enumerated().map { $0.element[0]["text"] as! String}
    return extractedText
} else {
        return [""];
    }

但没有得到实际的字符串。

1 个答案:

答案 0 :(得分:1)

您只需要转换为正确的类型,您的代码几乎是正确的。我在游乐场创建了这段代码,我可以获得所有的值。

let jsonString = "{\"language\":\"en\",\"textAngle\":0,\"orientation\":\"Up\",\"regions\":[{\"boundingBox\":\"96,29,244,474\",\"lines\":[{\"boundingBox\":\"96,29,58,12\",\"words\":[{\"boundingBox\":\"96,29,58,12\",\"text\":\"SG4207\"}]},{\"boundingBox\":\"97,64,159,16\",\"words\":[{\"boundingBox\":\"97,65,27,15\",\"text\":\"Eng\"},{\"boundingBox\":\"129,64,34,16\",\"text\":\"Lieh,\"},{\"boundingBox\":\"168,65,37,12\",\"text\":\"Yuen\"},{\"boundingBox\":\"212,65,44,15\",\"text\":\"Kwan,\"}]},{\"boundingBox\":\"97,99,243,16\",\"words\":[{\"boundingBox\":\"97,99,52,13\",\"text\":\"Mobile\"},{\"boundingBox\":\"154,99,64,13\",\"text\":\"Wireless\"},{\"boundingBox\":\"223,99,62,13\",\"text\":\"Solution\"},{\"boundingBox\":\"291,99,49,16\",\"text\":\"Design\"}]},{\"boundingBox\":\"97,134,117,16\",\"words\":[{\"boundingBox\":\"97,134,44,16\",\"text\":\"Darryl\"},{\"boundingBox\":\"147,134,27,13\",\"text\":\"and\"},{\"boundingBox\":\"179,134,35,16\",\"text\":\"Ajith\"}]},{\"boundingBox\":\"96,169,71,16\",\"words\":[{\"boundingBox\":\"96,169,71,16\",\"text\":\"Weekday\"}]},{\"boundingBox\":\"97,205,72,16\",\"words\":[{\"boundingBox\":\"97,205,72,16\",\"text\":\"(Monday)\"}]},{\"boundingBox\":\"96,241,80,15\",\"words\":[{\"boundingBox\":\"96,244,32,12\",\"text\":\"gam\"},{\"boundingBox\":\"133,248,5,1\",\"text\":\"-\"},{\"boundingBox\":\"143,241,33,15\",\"text\":\"5pm\"}]},{\"boundingBox\":\"96,275,72,13\",\"words\":[{\"boundingBox\":\"96,275,72,13\",\"text\":\"Weekend\"}]},{\"boundingBox\":\"97,310,77,16\",\"words\":[{\"boundingBox\":\"97,310,77,16\",\"text\":\"(Saturday)\"}]},{\"boundingBox\":\"96,347,80,15\",\"words\":[{\"boundingBox\":\"96,350,32,12\",\"text\":\"gam\"},{\"boundingBox\":\"133,354,5,1\",\"text\":\"-\"},{\"boundingBox\":\"143,347,33,15\",\"text\":\"5pm\"}]},{\"boundingBox\":\"97,382,41,15\",\"words\":[{\"boundingBox\":\"97,382,41,15\",\"text\":\"3-Apr\"}]},{\"boundingBox\":\"97,417,45,15\",\"words\":[{\"boundingBox\":\"97,417,8,12\",\"text\":\"1\"},{\"boundingBox\":\"115,417,27,15\",\"text\":\"Apr\"}]},{\"boundingBox\":\"97,452,48,15\",\"words\":[{\"boundingBox\":\"97,452,48,15\",\"text\":\"ID-Apr\"}]},{\"boundingBox\":\"96,488,42,15\",\"words\":[{\"boundingBox\":\"96,488,42,15\",\"text\":\"8-Apr\"}]}]}]}"

if let jsonDict = (try? JSONSerialization.jsonObject(with: Data(jsonString.utf8))) as? [String: Any] {
    if let regions = jsonDict["regions"] as? [[String: Any]] {
        for region in regions {
            if let lines = region["lines"] as? [[String: Any]] {
                for line in lines {
                    if let words = line["words"] as? [[String: Any]] {
                        for word in words {
                            if let text = word["text"] {
                                print(text)
                            }
                        }
                    }
                }
            }
        }
    }
}

输出:

Weekday
(Monday)
gam
-
5pm
Weekend
(Saturday)
gam
-
5pm
3-Apr
1
Apr
ID-Apr
8-Apr

显然,这里有很多嵌套,你可以使用地图等功能整理它并缩短它,但这个练习更多的是向你展示每个级别所需的转换。