zoukankan      html  css  js  c++  java
  • [Swift]SwiftyJSON的使用:解析JSON

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/ 
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/10709048.html 
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    用法

    初始化Initialization

    import SwiftyJSON
    let json = JSON(data: dataFromNetworking)

    或者

    1 let json = JSON(jsonObject)

    或者

    1 if let dataFromString = jsonString.data(using: .utf8, allowLossyConversion: false) {
    2     let json = JSON(data: dataFromString)
    3 }

    下标Subscript

     1 //从JSON数组中获取Double
     2 let name = json[0].double
     3 //从JSON数组中获取字符串数组
     4 let arrayNames =  json["users"].arrayValue.map {$0["name"].stringValue}
     5 //从JSON字典中获取字符串
     6 let name = json["name"].stringValue
     7 //使用元素的路径获取字符串
     8 let path: [JSONSubscriptType] = [1,"list",2,"name"]
     9 let name = json[path].string
    10 // 相同
    11 let name = json[1]["list"][2]["name"].string
    12 // 或者
    13 let name = json[1,"list",2,"name"].string
    14 //使用困难的方式
    15 let name = json[].string
    16 //使用自定义方式
    17 let keys:[JSONSubscriptType] = [1,"list",2,"name"]
    18 let name = json[keys].string

    循环Loop

    1 //如果json是.Dictionary
    2 for (key,subJson):(String, JSON) in json {
    3    // Do something you want
    4 }

    即使JSON是一个数组,第一个元素也总是一个String

    1 //如果json是.Array 
    2 // `index`是0 .. <json.count的字符串值
    3 for (index,subJson):(String, JSON) in json {
    4     // Do something you want
    5 }

    错误Error

    SwiftyJSON 4.x

    SwiftyJSON 4.x的引入称为枚举类型SwiftyJSONError,其中包括unsupportedTypeindexOutOfBoundselementTooDeepwrongTypenotExistinvalidJSON,在同一时间,ErrorDomain正在被更换SwiftyJSONError.errorDomain注意:这些旧的错误类型在SwiftyJSON 4.x中已弃用,将在以后的版本中删除。

    SwiftyJSON 3.x

    使用下标来获取/设置数组或字典中的值

    如果JSON是:

    • 一个数组,应用程序可能会崩溃“索引越界”。
    • 一本字典,它将被nil无故分配
    • 不是数组或字典,应用程序可能会崩溃“无法识别的选择器”异常。

    这在SwiftyJSON中永远不会发生。

     1 let json = JSON(["name", "age"])
     2 if let name = json[999].string {
     3     // Do something you want
     4 } else {
     5     print(json[999].error!) // "Array[999] is out of bounds"
     6 }
     7 
     8 let json = JSON(["name":"Jack", "age": 25])
     9 if let name = json["address"].string {
    10     // Do something you want
    11 } else {
    12     print(json["address"].error!) // "Dictionary["address"] does not exist"
    13 }
    14 
    15 let json = JSON(12345)
    16 if let age = json[0].string {
    17     // Do something you want
    18 } else {
    19     print(json[0])       // "Array[0] failure, It is not an array"
    20     print(json[0].error!) // "Array[0] failure, It is not an array"
    21 }
    22 
    23 if let name = json["name"].string {
    24     // Do something you want
    25 } else {
    26     print(json["name"])       // "Dictionary["name"] failure, It is not an dictionary"
    27     print(json["name"].error!) // "Dictionary["name"] failure, It is not an dictionary"
    28 }

    可选的getter

     1 // NSNumber
     2 if let id = json["user"]["favourites_count"].number {
     3    // Do something you want
     4 } else {
     5    // Print the error
     6    print(json["user"]["favourites_count"].error!)
     7 }
     8 
     9 // String
    10 if let id = json["user"]["name"].string {
    11    // Do something you want
    12 } else {
    13    // Print the error
    14    print(json["user"]["name"].error!)
    15 }
    16 
    17 // Bool
    18 if let id = json["user"]["is_translator"].bool {
    19    // Do something you want
    20 } else {
    21    // Print the error
    22    print(json["user"]["is_translator"].error!)
    23 }
    24 
    25 // Int
    26 if let id = json["user"]["id"].int {
    27    // Do something you want
    28 } else {
    29    // Print the error
    30    print(json["user"]["id"].error!)
    31 }
    32 ...

    非可选的getter

    非可选的getter被命名 xxxValue

    1 //如果不是Number或nil,则返回0
    2 let id: Int = json["id"].intValue
    3 //如果不是String或nil,则返回“”
    4 let name: String = json["name"].stringValue
    5 //如果不是数组或者为nil,则返回[]
    6 let list: Array<JSON> = json["list"].arrayValue
    7 //如果不是Dictionary或nil,返回[:]
    8 let user: Dictionary<String, JSON> = json["user"].dictionaryValue

    Setter

    1 json["name"] = JSON("new-name")
    2 json[0] = JSON(1)
    3 json["id"].int =  1234567890
    4 json["coordinate"].double =  8766.766
    5 json["name"].string =  "Jack"
    6 json.arrayObject = [1,2,3,4]
    7 json.dictionaryObject = ["name":"Jack", "age":25]

    原始对象Raw object

     1 let rawObject: Any = json.object
     2 let rawValue: Any = json.rawValue
     3 //将JSON转换为原始NSData
     4 do {
     5     let rawData = try json.rawData()
     6   //Do something you want
     7 } catch {
     8     print("Error (error)")
     9 }
    10 //将JSON转换为原始String
    11 if let rawString = json.rawString() {
    12   //Do something you want
    13 } else {
    14     print("json.rawString is nil")
    15 }

    存在Existence

    1 //显示是否在JSON中指定了值
    2 if json["name"].exists()

    文字敞篷车

    有关文字敞篷车的更多信息:Swift Literal Convertibles

     1 // StringLiteralConvertible
     2 let json: JSON = "I'm a json"
     3 // IntegerLiteralConvertible 
     4 let json: JSON =  12345
     5 // BooleanLiteralConvertible
     6 let json: JSON =  true
     7 // FloatLiteralConvertible
     8 let json: JSON =  2.8765
     9 // DictionaryLiteralConvertible
    10 let json: JSON =  ["I":"am", "a":"json"]
    11 // ArrayLiteralConvertible
    12 let json: JSON =  ["I", "am", "a", "json"]
    13 //使用数组中的下标
    14 var json: JSON =  [1,2,3]
    15 json[0] = 100
    16 json[1] = 200
    17 json[2] = 300
    18 json[999] = 300  //别担心,什么都不会发生
    19 //使用字典中的下标
    20 var json: JSON =  ["name": "Jack", "age": 25]
    21 json["name"] = "Mike"
    22 json["age"] = "25" // It's OK to set String
    23 json["address"] = "L.A." // Add the "address": "L.A." in json
    24 //数组和字典
    25 var json: JSON =  ["name": "Jack", "age": 25, "list": ["a", "b", "c", ["what": "this"]]]
    26 json["list"][3]["what"] = "that"
    27 json["list",3,"what"] = "that"
    28 let path: [JSONSubscriptType] = ["list",3,"what"]
    29 json[path] = "that"
    30 //使用其他JSON对象
    31 let user: JSON = ["username" : "Steve", "password": "supersecurepassword"]
    32 let auth: JSON = [
    33   "user": user.object, // use user.object instead of just user
    34   "apikey": "supersecretapitoken"
    35 ]

    合并Merging

    可以将一个JSON合并到另一个JSON中。将JSON合并到另一个JSON中会将所有非现有值添加到仅存在于otherJSON中的原始JSON。

    如果两个JSON都包含相同键的值,则大多数此值会被原始JSON覆盖,但有两种情况会提供一些特殊处理:

    • 如果两个值都是值,那么JSON.Type.arrayotherJSON中找到的数组会附加到原始JSON的数组值。
    • 如果两个值都是JSON.Type.dictionary两个JSON值,那么合并的方式与封装JSON的合并方式相同。

    如果JSON中的两个字段具有不同的类型,则该值将始终被覆盖。

    合并有两种不同的方式:merge修改原始JSON,而merged在副本上非破坏性地工作。

     1 let original: JSON = [
     2     "first_name": "John",
     3     "age": 20,
     4     "skills": ["Coding", "Reading"],
     5     "address": [
     6         "street": "Front St",
     7         "zip": "12345",
     8     ]
     9 ]
    10 
    11 let update: JSON = [
    12     "last_name": "Doe",
    13     "age": 21,
    14     "skills": ["Writing"],
    15     "address": [
    16         "zip": "12342",
    17         "city": "New York City"
    18     ]
    19 ]
    20 
    21 let updated = original.merge(with: update)
    22 // [
    23 //     "first_name": "John",
    24 //     "last_name": "Doe",
    25 //     "age": 21,
    26 //     "skills": ["Coding", "Reading", "Writing"],
    27 //     "address": [
    28 //         "street": "Front St",
    29 //         "zip": "12342",
    30 //         "city": "New York City"
    31 //     ]
    32 // ]

    字符串表示

    有两种选择:

    • 使用默认的Swift
    • 使用一个可以很好地处理选项的自定义表示nil"null"
    1 let dict = ["1":2, "2":"two", "3": nil] as [String: Any?]
    2 let json = JSON(dict)
    3 let representation = json.rawString(options: [.castNilToNSNull: true])
    4  // 表示为“{”1 “:2,”2 “:”two “,”3 “:null}”,表示{“1”:2,“2”:“two” “3”:空}

    Alamofire合作

    SwiftyJSON很好地包装了Alamofire JSON响应处理程序的结果:

    1 Alamofire.request(url, method: .get).validate().responseJSON { response in
    2     switch response.result {
    3     case .success(let value):
    4         let json = JSON(value)
    5         print("JSON: (json)")
    6     case .failure(let error):
    7         print(error)
    8     }
    9 }

    我们还提供了Alamofire的扩展,用于将NSData序列化为SwiftyJSON的JSON。

    请参阅:Alamofire-SwiftyJSON

    Moya合作

    SwiftyJSON将数据解析为JSON:

     1 let provider = MoyaProvider<Backend>()
     2 provider.request(.showProducts) { result in
     3     switch result {
     4     case let .success(moyaResponse):
     5         let data = moyaResponse.data
     6         let json = JSON(data: data) //将网络数据转换为json
     7         print(json)
     8     case let .failure(error):
     9         print("error: (error)")
    10     }
    11 }
  • 相关阅读:
    SpringSecurity03 基于内存验证
    SpringSecurity02 表单登录、SpringSecurity配置类
    SpringSecurity01 SpringSecurity环境搭建
    密码的加密和匹配
    SpringBoot05 数据操作03 -> JPA查询方法的规则定义
    SpringBoot05 数据操作02 -> JPA接口详解
    SpringBoot05 数据操作01 -> JPA的基本使用、基本使用02
    linux中目录操作<1>
    linux以行为单位进行读写操作
    linux以字符为单位进行读写操作
  • 原文地址:https://www.cnblogs.com/strengthen/p/10709048.html
Copyright © 2011-2022 走看看