在swift中解析json字符串
我是Swift编程新手,在json字符串解析方面遇到了问题。我的json字符串格式如下:在swift中解析json字符串,json,swift,Json,Swift,我是Swift编程新手,在json字符串解析方面遇到了问题。我的json字符串格式如下: { "fileName": "test", "display": "test", "children": [ { "fileName": "test2", "display": "test2", "children": [ { "fileName": "test3", "display": "te
{
"fileName": "test",
"display": "test",
"children": [
{
"fileName": "test2",
"display": "test2",
"children": [
{
"fileName": "test3",
"display": "test3",
"children": [
{
"fileName": "test4",
"display": "test4"
}
]
}
]
}
]
}
我想解析它以列出具有结构父对象和子对象的Dataobject,但到目前为止还没有成功。我的代码如下:
{
"fileName": "test",
"display": "test",
"children": [
{
"fileName": "test2",
"display": "test2",
"children": [
{
"fileName": "test3",
"display": "test3",
"children": [
{
"fileName": "test4",
"display": "test4"
}
]
}
]
}
]
}
儿童模型:
import Foundation
public struct Children {
public let fileName: String
public let display: String
public let children: [Children]
public init(lat: String, long: String, hourData: [Children]) {
self.fileName = lat
self.display = long
self.children = hourData
}
}
extension Children: JSONDecodable {
public init(decoder: JSONDecoder) throws {
self.fileName = try decoder.decode(key: "fileName")
self.display = try decoder.decode(key: "display")
self.children = try decoder.decode(key: "children")
}
}
import Foundation
public protocol JSONDecodable {
init(decoder: JSONDecoder) throws
}
public enum JSONDecoderError: Error {
case invalidData
case keyNotFound(String)
case keyPathNotFound(String)
}
public struct JSONDecoder {
typealias JSON = [String: AnyObject]
// MARK: - Properties
private let JSONData: JSON
// MARK: - Static Methods
public static func decode<T: JSONDecodable>(data: Data) throws -> T {
let decoder = try JSONDecoder(data: data)
return try T(decoder: decoder)
}
// MARK: - Initialization
public init(data: Data) throws {
if let JSONData = try JSONSerialization.jsonObject(with: data, options: []) as? JSON {
self.JSONData = JSONData
} else {
throw JSONDecoderError.invalidData
}
}
// MARK: -
private init(JSONData: JSON) {
self.JSONData = JSONData
}
// MARK: - Public Interface
func decode<T>(key: String) throws -> T {
if key.contains(".") {
return try value(forKeyPath: key)
}
guard let value: T = try? value(forKey: key) else { throw JSONDecoderError.keyNotFound(key) }
return value
}
func decode<T: JSONDecodable>(key: String) throws -> [T] {
if key.contains(".") {
return try value(forKeyPath: key)
}
guard let value: [T] = try? value(forKey: key) else { throw JSONDecoderError.keyNotFound(key) }
return value
}
// MARK: - Private Interface
private func value<T>(forKey key: String) throws -> T {
guard let value = JSONData[key] as? T else { throw JSONDecoderError.keyNotFound(key) }
return value
}
private func value<T: JSONDecodable>(forKey key: String) throws -> [T] {
if let value = JSONData[key] as? [JSON] {
return try value.map({ (partial) -> T in
let decoder = JSONDecoder(JSONData: partial)
return try T(decoder: decoder)
})
}
throw JSONDecoderError.keyNotFound(key)
}
// MARK: -
private func value<T>(forKeyPath keyPath: String) throws -> T {
var partial = JSONData
let keys = keyPath.components(separatedBy: ".")
for i in 0..<keys.count {
if i < keys.count - 1 {
if let partialJSONData = JSONData[keys[i]] as? JSON {
partial = partialJSONData
} else {
throw JSONDecoderError.invalidData
}
} else {
return try JSONDecoder(JSONData: partial).value(forKey: keys[i])
}
}
throw JSONDecoderError.keyPathNotFound(keyPath)
}
private func value<T: JSONDecodable>(forKeyPath keyPath: String) throws -> [T] {
var partial = JSONData
let keys = keyPath.components(separatedBy: ".")
for i in 0..<keys.count {
if i < keys.count - 1 {
if let partialJSONData = JSONData[keys[i]] as? JSON {
partial = partialJSONData
} else {
throw JSONDecoderError.invalidData
}
} else {
return try JSONDecoder(JSONData: partial).value(forKey: keys[i])
}
}
throw JSONDecoderError.keyPathNotFound(keyPath)
}
}
<代码>导入基础
公共结构儿童{
公共let文件名:String
公共let显示:字符串
公众儿童:[儿童]
public init(lat:String,long:String,hourData:[子项]){
self.fileName=lat
self.display=long
self.children=hourData
}
}
扩展子项:JSONDecodable{
公共init(解码器:JSONDecoder)抛出{
self.fileName=尝试解码(键:“fileName”)
self.display=尝试解码(键:“display”)
self.children=尝试解码(键:“children”)
}
}
进口基金会
公共协议JSONDECODATABLE{
init(解码器:JSONDecoder)抛出
}
公共枚举JSONDecoderError:错误{
案例无效数据
case keyNotFound(字符串)
案例keyPathNotFound(字符串)
}
公共结构JSONDecoder{
typealias JSON=[字符串:AnyObject]
//标记:-属性
私有let JSONData:JSON
//MARK:-静态方法
公共静态函数解码(数据:数据)抛出->T{
让解码器=尝试JSONDecoder(数据:数据)
返回try T(解码器:解码器)
}
//标记:-初始化
公共init(数据:data)抛出{
如果让JSONData=try JSONSerialization.jsonObject(使用:data,选项:[])作为?JSON{
self.JSONData=JSONData
}否则{
抛出JSONDecoderError.invalidData
}
}
//标记:-
私有init(JSONData:JSON){
self.JSONData=JSONData
}
//标记:-公共接口
func解码(键:字符串)抛出->T{
如果键包含(“.”){
返回try值(forKeyPath:key)
}
guard let value:T=try?value(forKey:key)else{throw jsondecordererror.keyNotFound(key)}
返回值
}
func解码(键:字符串)抛出->[T]{
如果键包含(“.”){
返回try值(forKeyPath:key)
}
保护let值:[T]=try?值(forKey:key)else{throw JSONDecoderError.keyNotFound(key)}
返回值
}
//MARK:-专用接口
私有函数值(forKey:String)抛出->T{
guard let value=JSONData[key]as?T else{throw jsondecordererror.keyNotFound(key)}
返回值
}
私有func值(forKey:String)抛出->[T]{
如果let value=JSONData[key]as?[JSON]{
返回try value.map({(部分)->T in
let decoder=JSONDecoder(JSONData:partial)
返回try T(解码器:解码器)
})
}
抛出JSONDecoderError.keyNotFound(键)
}
//标记:-
private func值(forKeyPath-keyPath:String)抛出->T{
var partial=JSONData
让keys=keyPath.components(以“.”分隔)
对于0中的i..[T]{
var partial=JSONData
让keys=keyPath.components(以“.”分隔)
对于0中的我,我强烈建议您尝试一下,我发现Swift中的JSON操作是一个真正的麻烦。SwiftyJSON解决了我遇到的许多问题。您搜索过Stackoverflow吗?如果您不知道如何阅读JSON结构,那么在Swift.@Francis.beuchamp中如何解析JSON有数千个相关问题-这非常简单y方式–解析它的工具是不相关的;-)我尝试了,但没有成功,所以我请求帮助:(