【问题标题】:How can I decode JSON with array and more JSON inside?如何解码带有数组和更多 JSON 的 JSON?
【发布时间】:2018-10-26 17:40:28
【问题描述】:

我最近开始使用 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'

【问题讨论】:

  • 我想建议这个:你可以使用伟大的SwiftJSON。 SwiftyJSON 使得在 Swift 中处理 JSON 数据变得很容易。 github.com/SwiftyJSON/SwiftyJSON
  • 你的可解码类是什么?请给我看你的代码。
  • 正如@Glenn 所说,SwiftyJSON 是一个很棒的工具。或者,您可以查看 Swift 的 Codable 协议。

标签: ios json swift parsing codable


【解决方案1】:
//try this it is working

let arrayMain=try?JSONSerialization.jsonObject(with:jsonData!,options:.mutableLeaves) as! Array<Any>


//print(arrayMain!)

if let firstDictionart=arrayMain![0] as? [String: Any] {
    if let insideFirstDict = firstDictionart["validation"] as? [String: Any]{
       print(insideFirstDict["bValid"]!)
         print( insideFirstDict["sDescription"]!)

    }

}
if let resultDictionary=arrayMain![1] as? [String: Any] {
    if let serviceDictionary = resultDictionary["result"] as? [String: Any] {
        for array in serviceDictionary["serviceCenter"] as! Array<Any>{
            if let infoDicti=array as? [String: Any]  {
                print( infoDicti["color"]!)
                print( infoDicti["make"]!)
                print(  infoDicti["color"]!)
                print( infoDicti["sTag"]!)
                print( infoDicti["sVin"]!)
                print( infoDicti["sMiles"]!)
                print( infoDicti["sVin"]!)
                print( infoDicti["model"]!)
                print( infoDicti["bDamage"]!)
                print( infoDicti["dDateTime"]!)         
            } 
        }
      }

    }

【讨论】:

    【解决方案2】:

    感谢大家的回答,这是我在这里的第一个问题,我觉得所有的帮助都很棒。最后我可以解决问题。也许不是最好的方法,但这里是代码:

    let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
            if error != nil{
                print("error=\(String(describing: error))")
                return
            }
            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)'")
                            let valid = JSONSerialization.isValidJSONObject(sc) // true
                            print(valid)
                            do {
                                let jsonData = try JSONSerialization.data(withJSONObject: sc, options: .prettyPrinted)
                                let serviceDecoded = try JSONSerialization.jsonObject(with: jsonData, options: [])
                                if let scJSON = serviceDecoded as? [String:String] {
                                    print ("--------------------------")
                                    print("'\(scJSON)' of type '\(type(of: scJSON))'")
                                    print ("--------------------------")
    
    
                                }
                            } catch {
                                print(error.localizedDescription)
                            }
    

    我想稍后你会尝试按照建议使用 Codable,但现在代码工作正常。再次感谢!

    【讨论】:

      【解决方案3】:

      您可以使用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)
          }
      }
      

      【讨论】:

        【解决方案4】:

        试试这个代码

        enum ParsingError: Error {
            case wrongFormat(String)
        }
        
        do {
            let jsonObject = try JSONSerialization.jsonObject(with: data!)
            guard let array = jsonObject as? [Any] else {
                throw ParsingError.wrongFormat("wrong root object")
            }
            guard array.count == 2 else {
                throw ParsingError.wrongFormat("array count != 2")
            }
            guard let dict = array[1] as? [String: Any] else {
                throw ParsingError.wrongFormat("can't parse dict from array")
            }
            guard let serviceCenters = (dict["result"] as? [String: Any])?["serviceCenter"] else {
                throw ParsingError.wrongFormat("can't parse serviceCenters")
            }
            guard let serviceCentersArray = serviceCenters as? [[String : Any]] else {
                throw ParsingError.wrongFormat("serviceCenters is not an array")
            }
        
            print("\(type(of: serviceCentersArray))\n", serviceCentersArray)
        
        } catch {
            print("json processing failed: \(error)")
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-21
          • 2021-07-13
          • 2022-11-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多