Murali Krishna
Murali Krishna

Reputation: 21

Parse JSON object to string array in swift

I have the following Json response.

{
  "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"
            }
          ]
        }
      ]
    }
  ]
}

Need to create a String array from the above json in Swift3. Tried the following

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 [""];
    }

But did not get the actual String.

Upvotes: 0

Views: 1767

Answers (1)

Scriptable
Scriptable

Reputation: 19750

You just need to cast to the correct types, your code is almost correct. I created this code in a playground and I can get all of the values.

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

Output:

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

Obviously there's alot of nesting here, you could tidy it up and shorten it using functions such as map, but this exercise was more to show you the casting that you need for each level.

Upvotes: 1

Related Questions