ios - 如何使用数组和内部更多 JSON 来解码 JSON?

标签 ios json swift parsing codable

我最近开始使用 swift。我需要解码下面的 json。

JSON 内部还有两个 JSON,第一个(验证)并不重要。第二个(结果)内部有一个 JSON 数组(serviceCenter)。我需要每个服务人员的信息。我尝试使用 servicecenter 作为可解码类来获取 servicenter 对象,但由于 JSON 没有正确的格式,我无法做到这一点。

[
  {
    "validation": {
      "bValid": true,
      "sDescription": "Access true."
    }
  },
  {
    "result": {
      "serviceCenter": [
        {
          "model": "Vanquish",
          "color": "Purple",
          "make": "Aston Martin",
          "sTag": "3666",
          "sVin": "6JDO2345",
          "sMiles": "3666",
          "bDamage": "1",
          "dDateTime": "04-17-2018 9:38 AM"
        },
        {
          "model": "F360",
          "color": "Red",
          "make": "Ferrari",
          "sTag": "0010",
          "sVin": "6JDO2347",
          "sMiles": "80000",
          "bDamage": "1",
          "dDateTime": "04-17-2018 9:25 AM"
        },
        {
          "model": "Vanquish",
          "color": "Purple",
          "make": "Aston Martin",
          "sTag": "0009",
          "sVin": "6JDO2345",
          "sMiles": "25000",
          "bDamage": "1",
          "dDateTime": "04-17-2018 9:23 AM"
        },
        {
          "model": "Vanquish",
          "color": "Purple",
          "make": "Aston Martin",
          "sTag": "0003",
          "sVin": "6JDO2345",
          "sMiles": "20000",
          "bDamage": "1",
          "dDateTime": "04-12-2018 10:37 AM"
        }
      ]
    }
  }
]

我尝试了很多,但还是做不到。

现在这是我的代码,有人可以帮助我吗

do {
    let parseoDatos = try JSONSerialization.jsonObject(with: data!) as! [AnyObject]
    let h = type(of: parseoDatos )
    print("'\(parseoDatos)' of type '\(h)'")
    let contenido = parseoDatos[1]["result"]

    if let services = contenido!! as? Dictionary<String, Array<Any>> {               
        for (_,serviceArray) in services {
            for sc in serviceArray{
                let h = type(of: sc )
                print("'\(sc)' of type '\(h)'")                        
            }
        }
    }
} catch {
    print("json processing failed")
}

print sc的结果是

{
    bDamage = 1;
    color = Purple;
    dDateTime = "04-17-2018 9:38 AM";
    make = "Aston Martin";
    model = Vanquish;
    sMiles = 3666;
    sTag = 3666;
    sVin = 6JDO2345;
}' of type '__NSDictionaryI'

最佳答案

您可以使用Codable

Initial 响应具有 InitialElement 数组,并且 InitialElement 是带有 validationresult< 的结构 ,结果可能是nil

不要忘记在url处添加您的网址

URLSession.shared.dataTask(with: url!) { (data, response, error) in

                     if  let initial =  try? JSONDecoder().decode([InitialElement].self, from: data){

                         // inital now have array of InitialElement and InitialElement is is struct with validation , result  , result may be nil


                        print(initial)
                        }
                    }.resume()

使用此数据模型:

import Foundation

struct InitialElement: Codable {
    let validation: Validation?
    let result: ResultData?
}

struct ResultData: Codable {
    let serviceCenter: [ServiceCenter]
}

struct ServiceCenter: Codable {
    let model, color, make, sTag: String
    let sVin, sMiles, bDamage, dDateTime: String
}

struct Validation: Codable {
    let bValid: Bool
    let sDescription: String
}


extension InitialElement {
    init(data: Data) throws {
        self = try JSONDecoder().decode(InitialElement.self, from: data)
    }
}

关于ios - 如何使用数组和内部更多 JSON 来解码 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50374749/

相关文章:

ios - iOS:应用从后台返回后,在访问EAOutputStream时出现SIGPIPE

json - 配置 ServiceStack.Text 以抛出无效的 JSON

javascript - 使用 Lodash 计算两个标准的总数

swift - 从文档目录中的本地镜像设置 UIImageView 的问题

swift - 使用 Swift 的 Bonjour 服务浏览器不获取服务信息

iphone - UIRefreshControl 无限期挂起

IOS8 UIscrollview 内部 uiview 问题

ios - 如何向 UITableViewCell 添加触摸识别器?

ios - UIViewController presentModalViewController : animated: causes SIGABRT

json - 如何使用 SwiftyJSON 获取此 subJson?