变更记录

序号 录入时间 录入人 备注
1 2015-04-09 Alfred Jiang -
2 2015-12-23 Alfred Jiang -

方案名称

JSON - 使用 JSONHelper 进行 JSON 数据解析

关键字

JSON \ JSONHelper \ 解析 \ 网络返回数据

需求场景

  1. 解析由网络返回的 JSON 格式数据

参考链接

  1. GitHub - JSONHelper

详细内容

#####1. 将 JSONHelper.swift 文件加入工程

#####2. 自定义的解析数据模型需要实现 Deserializable 协议

  1. 示例一

    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
    //定义
    internal struct Movie: Deserializable {
    var name: String? // You can also use let instead of var if you want.
    var releaseDate: NSDate?

    init(data: [String: AnyObject]) {
    name <-- data["name"]
    releaseDate <-- (data["release_date"], "yyyy-MM-dd") // Refer to the next section for more info.
    }
    }

    //使用
    AFHTTPRequestOperationManager().GET(
    "http://yoursite.com/movies/"
    parameters: nil,
    success: { operation, data in
    var movies: [Movie]?
    movies <-- data["movies"]

    if let movies = movies {
    // Response contained a movies array, and we deserialized it. Do what you want here.
    } else {
    // Server gave us a response but there was no "movies" key in it, so the movies variable
    // is equal to nil. Do some error handling here.
    }
    },
    failure: { operation, error in
    // Handle error.
    })
  2. 示例二

    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
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    //
    // User.swift
    // REX
    //
    // Created by Alfred Jiang on 3/19/15.
    // Copyright (c) 2015 REX. All rights reserved.
    //

    import UIKit

    var _currentUser: User?
    var currentUserKey = "currentUser"
    var userDidLoginNotification = "userDidLoginNotification"
    var userDidLogoutNotification = "userDidLogoutNotification"

    internal struct SideStandardProfileRequest: Deserializable {
    var url: String = ""

    init(data: [String: AnyObject]) {
    url <-- data["url"]
    }
    }

    internal struct Country: Deserializable {
    var code: String = ""

    init(data: [String: AnyObject]) {
    code <-- data["code"]
    }
    }

    internal struct Location: Deserializable {
    var country: Country?
    var name: String = ""

    init(data: [String: AnyObject]) {
    name <-- data["name"]
    country <-- data["country"]
    }
    }

    class User : NSObject , Deserializable{
    var id: String!
    var firstName: String?
    var lastName: String?
    var headline: String?
    var profileImageUrl: String?
    var email: String?
    var industry: String?
    var countryCode : String?
    var locationAddress : String?
    var siteStandardProfileRequest: String?

    var data: NSDictionary?

    required init(data: [String: AnyObject])
    {
    self.data = data
    id <-- data["id"]
    firstName <-- data["firstName"]
    lastName <-- data["lastName"]
    headline <-- data["headline"]
    profileImageUrl <-- data["pictureUrl"]
    email <-- data["emailAddress"]
    industry <-- data["industry"]

    var aLocation: Location?
    aLocation <-- data["location"]
    countryCode = aLocation?.country?.code
    locationAddress = aLocation?.name

    var aSiteStandardProfileRequest : SideStandardProfileRequest?
    aSiteStandardProfileRequest <-- data["siteStandardProfileRequest"]

    siteStandardProfileRequest = aSiteStandardProfileRequest?.url
    }

    class var currentUser: User? {
    get {
    if (_currentUser == nil) {
    var data = NSUserDefaults.standardUserDefaults().objectForKey(currentUserKey) as? NSData
    if (data != nil) {
    var dict = NSJSONSerialization.JSONObjectWithData(data!, options: nil, error: nil) as NSDictionary
    _currentUser <-- dict
    }
    }
    return _currentUser
    }
    set(user) {
    _currentUser = user

    if (_currentUser != nil) {
    var data = NSJSONSerialization.dataWithJSONObject(user!.data!, options: nil, error: nil)
    NSUserDefaults.standardUserDefaults().setObject(data, forKey: currentUserKey)
    } else {
    NSUserDefaults.standardUserDefaults().setObject(nil, forKey: currentUserKey)
    }
    NSUserDefaults.standardUserDefaults().synchronize()
    }
    }

    /*

    "{\"userID\":\"10001\",\"userName\":\"10001\",\"firstName\":\"4\",\"lastName\":\"4\",\"displayName\":null,\"email\":\"aaa@aaa.com\",\"password\":\"4\",\"address\":\"4\",\"headline\":null,\"profileImageUrl\":null,\"industry\":null,\"countryCode\":null,\"siteStandardProfileRequest\":null,\"roleId\":\"4\"}"

    */

    func toJSONDict() -> NSDictionary
    {
    let aDict : NSMutableDictionary = NSMutableDictionary()

    if let id = self.id {
    aDict.setValue(id, forKey: "userName1")
    }

    if let firstName = self.firstName {
    aDict.setValue(firstName, forKey: "firstName")
    }

    if let lastName = self.lastName {
    aDict.setValue(lastName, forKey: "lastName")
    }

    if let headline = self.headline {
    aDict.setValue(headline, forKey: "headline")
    }

    if let profileImageUrl = self.profileImageUrl {
    aDict.setValue(profileImageUrl, forKey: "profileImageUrl")
    }

    if let email = self.email {
    aDict.setValue(email, forKey: "email")
    }

    if let industry = self.industry {
    aDict.setValue(industry, forKey: "industry")
    }

    if let countryCode = self.countryCode {
    aDict.setValue(countryCode, forKey: "countryCode")
    }

    if let locationAddress = self.locationAddress {
    aDict.setValue(locationAddress, forKey: "address")
    }

    if let siteStandardProfileRequest = self.siteStandardProfileRequest {
    aDict.setValue(siteStandardProfileRequest, forKey: "siteStandardProfileRequest")
    }

    return aDict
    }
    }

#####3. 更多详细用法参考 GitHub - JSONHelper

效果图

(无)

备注

  1. 遵循 Deserializable 协议的属性应该是option类型