我的API响应如下所示
{
"error": false,
"id": "6",
"user_id": 7,
"users": [
{
"user_id": 1,
"username": "spiderman"
},
{
"user_id": 7,
"username": "wonderwoman"
}
],
"info": [
{
"id": 471,
"message": "abc",
"age": 10,
}
]
}
我知道如何在NSOject中初始化id
,user_id
和error
的值。但是我不知道如何在同一个NSObject类中初始化users
和info
的数组。
现在我像这样初始化JSON
import UIKit
import SwiftyJSON
class MyItem: NSObject {
var userId : Int
var error : Bool
var id : Int
init?(dict: [String :JSON]) {
self.id = dict["id"]?.int ?? 0
self.error = dict["error"]?.bool ?? false
self.userId = dict["userId"]?.int ?? 0
}
}
现在问题是我不知道如何初始化users
和info
字典中的数据。我该如何安排它以及如何在其他类中使用它
请举个例子。
答案 0 :(得分:1)
使用如下,
根类: -
var ctx = document.getElementById("myChart2").getContext("2d");
var myObjBar = new Chart(ctx, {
type: 'bar',
data: {
labels: markers[0].templabel,
datasets: [{
data: markers[0].batterytemp,
backgroundColor: 'rgba(255, 99, 132, 0.2)',
borderColor: 'rgba(255,99,132,1)',
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}],
xAxes: [{
barThickness: 30
}]
}
}
});
alert("welcome");
var dataset = myObjBar.data.datasets[0];
alert(dataset.data.length);
var maxt = markers[0].maxt;
alert(maxt);
var i;
var color;
for (i = 0; i < dataset.data.length; i++) {
alert(dataset.data[i]);
if (dataset.data[i] < markers[0].mint) {
alert("inside if")
/* dataset.backgroundColor[i] = "red"; */
} else {
alert("inside elseif")
}
}
myObjBar.update();
用户类: -
import Foundation
import SwiftyJSON
class RootClass : NSObject, NSCoding{
var error : Bool!
var id : String!
var info : [Info]!
var userId : Int!
var users : [User]!
/**
* Instantiate the instance using the passed json values to set the properties values
*/
init(fromJson json: JSON!){
if json.isEmpty{
return
}
error = json["error"].boolValue
id = json["id"].stringValue
info = [Info]()
let infoArray = json["info"].arrayValue
for infoJson in infoArray{
let value = Info(fromJson: infoJson)
info.append(value)
}
userId = json["user_id"].intValue
users = [User]()
let usersArray = json["users"].arrayValue
for usersJson in usersArray{
let value = User(fromJson: usersJson)
users.append(value)
}
}
/**
* Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
*/
func toDictionary() -> [String:Any]
{
let dictionary = [String:Any]()
if error != nil{
dictionary["error"] = error
}
if id != nil{
dictionary["id"] = id
}
if info != nil{
var dictionaryElements = [[String:Any]]()
for infoElement in info {
dictionaryElements.append(infoElement.toDictionary())
}
dictionary["info"] = dictionaryElements
}
if userId != nil{
dictionary["user_id"] = userId
}
if users != nil{
var dictionaryElements = [[String:Any]]()
for usersElement in users {
dictionaryElements.append(usersElement.toDictionary())
}
dictionary["users"] = dictionaryElements
}
return dictionary
}
/**
* NSCoding required initializer.
* Fills the data from the passed decoder
*/
@objc required init(coder aDecoder: NSCoder)
{
error = aDecoder.decodeObject(forKey: "error") as? Bool
id = aDecoder.decodeObject(forKey: "id") as? String
info = aDecoder.decodeObject(forKey: "info") as? [Info]
userId = aDecoder.decodeObject(forKey: "user_id") as? Int
users = aDecoder.decodeObject(forKey: "users") as? [User]
}
/**
* NSCoding required method.
* Encodes mode properties into the decoder
*/
func encode(with aCoder: NSCoder)
{
if error != nil{
aCoder.encode(error, forKey: "error")
}
if id != nil{
aCoder.encode(id, forKey: "id")
}
if info != nil{
aCoder.encode(info, forKey: "info")
}
if userId != nil{
aCoder.encode(userId, forKey: "user_id")
}
if users != nil{
aCoder.encode(users, forKey: "users")
}
}
}
信息类: -
import Foundation
import SwiftyJSON
class User : NSObject, NSCoding{
var userId : Int!
var username : String!
/**
* Instantiate the instance using the passed json values to set the properties values
*/
init(fromJson json: JSON!){
if json.isEmpty{
return
}
userId = json["user_id"].intValue
username = json["username"].stringValue
}
/**
* Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
*/
func toDictionary() -> [String:Any]
{
let dictionary = [String:Any]()
if userId != nil{
dictionary["user_id"] = userId
}
if username != nil{
dictionary["username"] = username
}
return dictionary
}
/**
* NSCoding required initializer.
* Fills the data from the passed decoder
*/
@objc required init(coder aDecoder: NSCoder)
{
userId = aDecoder.decodeObject(forKey: "user_id") as? Int
username = aDecoder.decodeObject(forKey: "username") as? String
}
/**
* NSCoding required method.
* Encodes mode properties into the decoder
*/
func encode(with aCoder: NSCoder)
{
if userId != nil{
aCoder.encode(userId, forKey: "user_id")
}
if username != nil{
aCoder.encode(username, forKey: "username")
}
}
}
答案 1 :(得分:0)
这样做,
class MyItem: NSObject {
var userId : Int
var error : Bool
var id : Int
var users : [[String:Any]] = []
var info : [[String:Any]] = []
init?(dict: [String :JSON]) {
self.id = dict["id"]?.int ?? 0
self.error = dict["error"]?.bool ?? false
self.userId = dict["userId"]?.int ?? 0
self.users = dict["users"] ?? []
self.info = dict["info"] ?? []
}
}
答案 2 :(得分:0)
执行此操作的最佳方法是为用户和信息创建2个不同的类,如下所示:
class MyItem : NSObject {
var error : Bool!
var id : String!
var info : [Info]!
var userId : Int!
var users : [User]!
init(fromJson json: JSON!){
if json == nil{
return
}
error = json["error"].boolValue
id = json["id"].stringValue
info = [Info]()
let infoArray = json["info"].arrayValue
for infoJson in infoArray{
let value = Info(fromJson: infoJson)
info.append(value)
}
userId = json["user_id"].intValue
users = [User]()
let usersArray = json["users"].arrayValue
for usersJson in usersArray{
let value = User(fromJson: usersJson)
users.append(value)
}
}
}
class User : NSObject {
var userId : Int!
var username : String!
init(fromJson json: JSON!){
if json == nil{
return
}
userId = json["user_id"].intValue
username = json["username"].stringValue
}
}
class Info : NSObject {
var age : Int!
var id : Int!
var message : String!
init(fromJson json: JSON!){
if json == nil{
return
}
age = json["age"].intValue
id = json["id"].intValue
message = json["message"].stringValue
}
}
通过这样做,您将能够直接访问用户和信息的值,例如:MyItem.users[index].userId
答案 3 :(得分:0)
SwiftyJSON
的开发人员没有冒犯,它是一个很棒的库,但在Swift 4中解码JSON SwiftyJSON
已经过时了。
使用Decodable
协议,您无需任何额外代码即可解码JSON。
创建一个包含两个子结构的结构,编码键只需要将 snake_case 映射到 camelCase 。
struct MyItem: Decodable {
private enum CodingKeys: String, CodingKey { case userId = "user_id", error, id, users, info}
let userId : Int
let error : Bool
let id : String
let users : [User]
let info : [Info]
struct User : Decodable {
private enum CodingKeys: String, CodingKey { case userId = "user_id", username}
let userId : Int
let username : String
}
struct Info : Decodable {
let message : String
let id, age : Int
}
}
现在解码JSON
let jsonString = """
{
"error": false,
"id": "6",
"user_id": 7,
"users": [{"user_id": 1, "username": "spiderman"},{"user_id": 7,"username": "wonderwoman"}],
"info": [{"id": 471,"message": "abc", "age": 10}]
}
"""
do {
let data = Data(jsonString.utf8)
let decoder = JSONDecoder()
let result = try JSONDecoder().decode(MyItem.self, from: data)
for user in result.users {
print(user.userId, user.username)
}
} catch { print(error) }