Swift 3 - How to convert an array of structs that contain structs to JSON?
我有一个要转换为 JSON 字符串的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | struct Field{ var name: String var center: LatLng var perimeter: [LatLng] func toDictionary() -> [String : Any]{ let dict: [String : Any] = ["name":self.name, "center":self.center.toDictionary(), "perimeter": ppsToDictArray()] return dict } fileprivate func ppsToDictArray() -> [Any]{ var data = [Any]() for pp in perimeterPoints{ data.append(pp.toDictionary()) } return data } } |
和
1 2 3 4 5 6 7 8 9 10 11 12 | struct LatLng{ let latitude: Double let longitude: Double func toDictionary() -> [String : Any]{ let dict: [String : Any] = ["latitude": self.latitude, "longitude": self.longitude] return dict } } |
这是我尝试将数组转换为 JSON 的地方:
1 2 3 4 5 6 7 8 | //selectedFields is a [Field] populated with some Fields let dicArray = selectedFields.map{$0.toDictionary()} if let data = try? JSONSerialization.data(withJSONObject: dicArray, options: .prettyPrinted){ let str = String(bytes: data, encoding: .utf8) print(str) //Prints a string of"\ \ " } |
如何将此类和数组转换为 JSON 字符串?我尝试了一些类似这个答案的东西,但它打印为
\
]")"
编辑:
我编辑了上面的代码,以代表一个更完整的示例,说明我正在处理的内容,以响应查看更多工作的请求。我最初并没有包括所有这些,因为我不是在问如何修复我现有的代码,而是更多关于如何使用嵌套结构进行处理的示例。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | struct LatLng { let latitude: Double let longitude: Double func getJSON() -> NSMutableDictionary { let dict = NSMutableDictionary() dict.setValue(latitude, forKey:"latitude") dict.setValue(longitude, forKey:"longitude") return dict } } struct Field { var name: String var center: LatLng var perimeter: [LatLng] func getJSON() -> NSMutableDictionary { let values = NSMutableDictionary() var perimeterArray = Array<NSMutableDictionary>() for item in perimeter { perimeterArray.append(item.getJSON()) } values.setValue(name, forKey:"name") values.setValue(center.getJSON(), forKey:"center") values.setValue(perimeterArray, forKey:"perimeter") return values } } let peri = [LatLng(latitude: 10.0, longitude: 10.0), LatLng(latitude: 20.0, longitude: 20.0)] let center = LatLng(latitude: 15.0, longitude: 15.0) let field = Field(name:"test", center: center, perimeter: peri) let json = try NSJSONSerialization.dataWithJSONObject(field.getJSON(), options: .PrettyPrinted) let jsonString = NSString(data: json, encoding: NSUTF8StringEncoding) print(jsonString) //PRINTS THE FOLLOWING OUTPUT Optional({ "name" :"test", "center" : { "longitude" : 15, "latitude" : 15 }, "perimeter" : [{ "longitude" : 10, "latitude" : 10 }, { "longitude" : 20, "latitude" : 20 }] }) |
更新
为了序列化一个 Field 对象的数组,你可以这样做。
1 2 3 4 5 6 7 8 | let field1 = Field(name:"value1", center: center, perimeter: peri) let field2 = Field(name:"value2", center: center, perimeter: peri) let field3 = Field(name:"value3", center: center, perimeter: peri) let fieldArray = [field1.getJSON(), field2.getJSON(), field3.getJSON()] let json = try NSJSONSerialization.dataWithJSONObject(fieldArray, options: .PrettyPrinted) let jsonString = NSString(data: json, encoding: NSUTF8StringEncoding) |
请注意,这只是一个快速的解决方案,而不是最好的解决方案。这只是为了让您了解它将如何进行。我相信您将能够改进它。