如何为以下JSON数据创建模型类并解析它?

前端之家收集整理的这篇文章主要介绍了如何为以下JSON数据创建模型类并解析它?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的 JSON数据
{
 "addon_items" : [
                     {
                      "aname" : "","id" : "2588","name" : "Plain Nan","order" : "1","aid" : "259","Sub_Add_Items" : "","icon" : "","status" : "1","next" : "0","price" : "0.60"
                     },{
                      "aname" : "","id" : "2589","name" : "Pitta Bread","order" : "2","price" : "0.00"
                    }

                   ],"addon" : {
             "description" : "Please choose your Nan bread","aname" : "","id" : "259","limit" : "1","special_addon" : "","next" : "165"
           }
 }

我创建了三个名为AddOnResponse,AddOn,AddOnItems的类模型,如下所示:

AddOnResponse类模型

class AddOnResponse {

var addon: Array<String>?
var addonitems: Array<AnyObject>?

init(addon:Array<String>?,addonitems: Array<AnyObject>?){
    self.addon = addon
    self.addonitems = addonitems
 }
}

AddOn类模型

class AddOn {


var id: Int?
var icon: String?
var desc: String?
var limit: Int?
var next: Int?
var aname: String?
var specialaddon: Int?

init(id: Int?,icon: String?,desc: String?,limit: Int?,next: Int?,aname: String?,specialaddon: Int?){

    self.id = id
    self.icon = icon
    self.desc = desc
    self.limit = limit
    self.next = next
    self.aname = aname
    self.specialaddon = specialaddon

  }
 }

AddOnItems类模型

class AddOnItems {


var id: Int?
var aid: Int?
var name: String?
var price: Int?
var order: Int?
var status: Int?
var next: Int?
var aname: String?
var subaddItems: Int?
var icon: String?

init(id: Int?,aid: Int?,name: String?,price: Int?,order: Int?,status: Int?,subaddItems: Int?,icon: String?){
    self.id = id
    self.aid = aid
    self.name = name
    self.price = price
    self.order = order
    self.status = status
    self.next = next
    self.aname = aname
    self.subaddItems = subaddItems
    self.icon = icon
   }
 }

现在我使用Alamofire获取我的JSON数据,但是当使用对象接受dat到类模型时,我获得了nil值.

var addonResponses = [AddOnResponse]()

    Alamofire.request(.GET,myAddOnUrl)
        .validate()
        .responseJSON
        {   response in
            switch response.result
            {
            case .Success:
                if let value = response.result.value{
                    let json = JSON(value)
                    print(json)
                    print(json["addon"].arrayValue)


           for(_,content) in json{
               let addOnRes = AddOnResponse(addon:content["addon"].arrayValue,addonitems:content["addon_items"].Arrayobject)

                        print(self.addonResponses.count)
                        print(addOnRes.addon)
                        print(addOnRes.addonitems)
                    }
                }

addon和addonitems数据是零,为什么?

解决方法

在完成JSON响应之后,我看到的是你得到的对象有两个节点(或属性).
首先是“addon_items”,它有一个数组,你已经为它创建了一个正确的AddOnItems类.
第二个 – “插件”:这里的这个键是指’Dictionary’而不是数组.

因此,要将响应存储在AddOnResponse对象中,请尝试以下代码.

Alamofire.request(.GET,myAddOnUrl).validate().reponseJSON { response in
    switch resonse.result {
    case .Success:
       if let value = response.result.value {
           let json = JSON(value)
           let responseDictionary = json.dictionaryValue as? [String: AnyObject]
           let addOnRes = AddOnResponse(addon:responseDictionary["addon"].dictionaryValue,addonitems:responseDictionary["addon_items"].arrayValue)
       }
    case .Failure:
       break
    } 
}

还要更改AddOnResponse类

class AddOnResponse {
    var addon: [String: AnyObject]?
    var addonitems: Array<AnyObject>?

    init(addon:[String: AnyObject]?,addonitems: Array<AnyObject>?){
        self.addon = addon
        self.addonitems = addonitems
    }
}

TL; DR
您的JSON响应与您在应用中制作的模型不完全对应.仔细检查你的json响应的“addon”键,它有一个字典对象,而不是一个ARRAY,因此可以制作你的模型类.

编辑:纠正错误以指出投射错误.
我现在建议的是传递`add_on’键的JSON对象.在AddOn类中更改初始化程序,以便它采用JSON对象.然后使用初始化它们.
AddOn类初始化程序

init(json: JSON) {
    id = json["id"].intValue
    name = json["name"].stringValue
    // and so on
}

同样地,AddOnItems也是如此.并且在AddOnResponse初始化器中循环迭代AddOnItems的JSON对象.初始化它并附加到addOnItems数组属性.抱歉,目前无法为其编写代码.有时间限制.

原文链接:https://www.f2er.com/js/150116.html

猜你在找的JavaScript相关文章