2017-05-07 111 views
0

我有以下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

爲什麼在Swift 3中使用'NSArray'和'NSDictionary'?使用Swift數組和字典。 – rmaddy

回答

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 

顯然有很多嵌套在這裏,你可以把它打掃乾淨和使用功能,如地圖縮短,但是,這個運動是更告訴你,你需要的鑄造爲每個級別。

+0

Thankank Leo Dabus。這很有效。 –