Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在SwiftyJson中,在方括号之前解析没有键的数组?_Ios_Arrays_Swift_Swifty Json - Fatal编程技术网

Ios 在SwiftyJson中,在方括号之前解析没有键的数组?

Ios 在SwiftyJson中,在方括号之前解析没有键的数组?,ios,arrays,swift,swifty-json,Ios,Arrays,Swift,Swifty Json,我正在从服务器获取国家/地区列表。服务器响应如下所示 [ { "CountryID": 2, "Name": "Afghanistan", "Code": "AFG", "CreatedDate": "2018-01-09T02:05:02.08" }, { "CountryID": 3, "Name": "Aland Islands", "Code": "AL

我正在从服务器获取国家/地区列表。服务器响应如下所示

[
    {
        "CountryID": 2,
        "Name": "Afghanistan",
        "Code": "AFG",
        "CreatedDate": "2018-01-09T02:05:02.08"
    },
    {
        "CountryID": 3,
        "Name": "Aland Islands",
        "Code": "ALA",
        "CreatedDate": "2018-01-09T02:05:02.08"
    }
]
if let value = response.result.value {
   let json = JSON(value)                        
   let countryListData = CountryList(fromJson: json)
   completionHandler(true, countryListData)
}
class CountryList {
    var countries: [Country]!
    init(fromJson json: JSON!) {
        let countryArray = json[0].arrayValue
        for countryJson in countryArray {
            let value = Country(fromJson: countryJson)
            countries.append(value)
        }
    }
}

class Country {
    var code : String!
    var countryID : Int!
    var createdDate : String!
    var name : String!
    init(fromJson json: JSON!){
        if json == nil{
            return
        }
        code = json["Code"].stringValue
        countryID = json["CountryID"].intValue
        createdDate = json["CreatedDate"].stringValue
        name = json["Name"].stringValue
    }
}
我使用SwiftyJSON将响应转换为Json,如下所示

[
    {
        "CountryID": 2,
        "Name": "Afghanistan",
        "Code": "AFG",
        "CreatedDate": "2018-01-09T02:05:02.08"
    },
    {
        "CountryID": 3,
        "Name": "Aland Islands",
        "Code": "ALA",
        "CreatedDate": "2018-01-09T02:05:02.08"
    }
]
if let value = response.result.value {
   let json = JSON(value)                        
   let countryListData = CountryList(fromJson: json)
   completionHandler(true, countryListData)
}
class CountryList {
    var countries: [Country]!
    init(fromJson json: JSON!) {
        let countryArray = json[0].arrayValue
        for countryJson in countryArray {
            let value = Country(fromJson: countryJson)
            countries.append(value)
        }
    }
}

class Country {
    var code : String!
    var countryID : Int!
    var createdDate : String!
    var name : String!
    init(fromJson json: JSON!){
        if json == nil{
            return
        }
        code = json["Code"].stringValue
        countryID = json["CountryID"].intValue
        createdDate = json["CreatedDate"].stringValue
        name = json["Name"].stringValue
    }
}
国家名单课应该是这样的

[
    {
        "CountryID": 2,
        "Name": "Afghanistan",
        "Code": "AFG",
        "CreatedDate": "2018-01-09T02:05:02.08"
    },
    {
        "CountryID": 3,
        "Name": "Aland Islands",
        "Code": "ALA",
        "CreatedDate": "2018-01-09T02:05:02.08"
    }
]
if let value = response.result.value {
   let json = JSON(value)                        
   let countryListData = CountryList(fromJson: json)
   completionHandler(true, countryListData)
}
class CountryList {
    var countries: [Country]!
    init(fromJson json: JSON!) {
        let countryArray = json[0].arrayValue
        for countryJson in countryArray {
            let value = Country(fromJson: countryJson)
            countries.append(value)
        }
    }
}

class Country {
    var code : String!
    var countryID : Int!
    var createdDate : String!
    var name : String!
    init(fromJson json: JSON!){
        if json == nil{
            return
        }
        code = json["Code"].stringValue
        countryID = json["CountryID"].intValue
        createdDate = json["CreatedDate"].stringValue
        name = json["Name"].stringValue
    }
}
在SwiftyJson中,如果没有方括号前的键,如何解析此数组?它没有正确地给出数组对象

我知道这是以正常的方式进行的,就像将响应转换为字典一样。但客户建议使用SwiftyJson。所以我只想用迅捷JSON试试


给我一些建议,不要把这个问题标为重复问题。因为我没有从互联网上获得任何参考资料来使用SwiftyJson转换此内容。

您的课程中有两个问题
CountryList

class CountryList {
  // 1. the countries var is not initialized
  // var countries: [Country]!
  // Initialize it like below
  var countries = [Country]()
  init(fromJson json: JSON!) {
    // 2 issue is that json itself is an array so no need of doing json[0].arrayValue
    let countryArray = json.arrayValue
    for countryJson in countryArray {
      let value = Country(fromJson: countryJson)
      countries.append(value)
    }
  }
}

我自己找到了答案。我把反应变成了这样

do {
   let array = try JSONSerialization.jsonObject(with: response.data!) as? NSArray
   let countryListData = CountryList(fromArray: array!)
   completionHandler(true, countryListData)
} catch {
   print("Exception occured \(error))")
}
class CountryList {
    var countries = [Country]()
    init(fromArray array: NSArray!) {
        for countryJson in array {
            let value = Country(fromJson: countryJson)
            countries.append(value)
        }
    }
}

class Country {
    var code : String!
    var countryID : Int!
    var createdDate : String!
    var name : String!
    init(fromJson json: JSON!){
        if json == nil{
            return
        }
        code = json["Code"].stringValue
        countryID = json["CountryID"].intValue
        createdDate = json["CreatedDate"].stringValue
        name = json["Name"].stringValue
    }
}
这样改变了CountryList类

do {
   let array = try JSONSerialization.jsonObject(with: response.data!) as? NSArray
   let countryListData = CountryList(fromArray: array!)
   completionHandler(true, countryListData)
} catch {
   print("Exception occured \(error))")
}
class CountryList {
    var countries = [Country]()
    init(fromArray array: NSArray!) {
        for countryJson in array {
            let value = Country(fromJson: countryJson)
            countries.append(value)
        }
    }
}

class Country {
    var code : String!
    var countryID : Int!
    var createdDate : String!
    var name : String!
    init(fromJson json: JSON!){
        if json == nil{
            return
        }
        code = json["Code"].stringValue
        countryID = json["CountryID"].intValue
        createdDate = json["CreatedDate"].stringValue
        name = json["Name"].stringValue
    }
}

现在它的工作是我所期望的。感谢您的评论和回答。

如果在方括号前没有键,解析数组是什么意思?请尝试用json替换json[0]。arrayValue。arrayValue@PrashantTukadiya-谢谢你的快速回复。我也这样尝试过,但现在也得到了一个空数组。为什么要写这个
让countryArray=json[0]。arrayValue
,你的数据是数组,所以不需要
json[0]。arrayValue
,只使用
json.arrayValue
或者直接在json中为(index,subson):(String,json)尝试
{//Do your want}
感谢您的快速回复。json.arrayValue本身就是emply。但当我打印json时,它会这样打印。[{“CountryID”:2,“名称”:“阿富汗”,“代码”:“AFG”,“CreatedDate”:“2018-01-09T02:05:02.08”},{“CountryID”:3,“名称”:“阿拉德群岛”,“代码”:“阿拉”,“CreatedDate”:“2018-01-09T02:05:02.08”},…]请将您的整个json发布在这里:在线保存后将链接发送给我。对于您在QuesitionTanks中为您的Inder工作提供的json,上述更改对我很有用。这是您的json看起来很完美的链接,当我用我的演示项目替换它时,它在我的演示项目中工作得很好:谢谢。我现在检查一下,然后告诉你。