已获取 json 文件,因为 NSArray 仅包含第一个字符
Fetched json file as NSArray contains only the first character
我有一个 json file
看起来像这样:
[
{
"tn_newsid": 144377,
"tn_midashi": "知床半島とオホーツク海の絶景が広がる",
"tn_name": "北海道立オホーツク公園オートキャンプ場てんとらんど",
"tn_ktel": "0152-45-27",
"tn_url": "",
"tn_kdo": 144.239526856306,
"tn_ido": 43.9880220633556,
"tn_im1name": "144377_1.jpg",
"tn_im2name": "144377_2.jpg",
"tn_im3name": "144377_3.jpg",
"tn_im4name": "144377_4.jpg",
"type": 0,
"nico_recommended": 0
},
{
// etc ....
直到现在我都是通过截取这段代码获取数据
seasonInfoService.seasonServerDataJson(jsoncompleted: { () in
let responseData: Data = self.seasonInfoService.UpdateJsonString.data(using: String.Encoding.utf8)!
do{
let items = try JSONSerialization.jsonObject(with: responseData) as! Dictionary<String,Any>
但是 json file
以 [
开头,所以我需要将 Dictionary<String,Any>
更改为 NSArray
因为我收到这个错误代码
Could not cast value of type '__NSArrayI' (0x7fff86cd0128) to 'NSDictionary'
我的代码现在看起来像
facilitiesService.facilitiesServerDataJson(urlToFetch: url_, jsoncompleted: { () in
let responseData: Data = self.facilitiesService.UpdateJsonString.data(using: String.Encoding.utf8)!
do{
let items = try JSONSerialization.jsonObject(with: responseData) as! NSArray
print(items)
但是打印语句只显示 [
,请问如何将其更改为正确的词典列表?
首先创建一个符合Decodable的结构体。 good/necessary映射JSON数据
struct MyStruct: Decodable {
let tn_newsid: Int
let tn_midashi: String
let tn_name: String
let tn_ktel: String
let tn_url: String
let tn_kdo: Double
let tn_ido: Double
let tn_im1name: String
let tn_im2name: String
let tn_im3name: String
let tn_im4name: String
let type: Int
let nico_recommended: Int
}
那么假设您的 JSON 回复如下所示:
let responseData: String = """
[
{
"tn_newsid": 1477,
"tn_midashi": "知床半島とオホーツク海の絶景が広がる",
"tn_name": "北海道立オホーツク公園オートキャンプ場てんとらんど",
"tn_ktel": "0152-45-27",
"tn_url": "",
"tn_kdo": 144.239526856306,
"tn_ido": 43.9880220633556,
"tn_im1name": "144377_1.jpg",
"tn_im2name": "144377_2.jpg",
"tn_im3name": "144377_3.jpg",
"tn_im4name": "144377_4.jpg",
"type": 0,
"nico_recommended": 2
},
{
"tn_newsid": 144377,
"tn_midashi": "知ホーツク海の絶景が広がる",
"tn_name": "北海道立オホートキャンプ場てんとらんど",
"tn_ktel": "0152-45-27",
"tn_url": "",
"tn_kdo": 144.239526856306,
"tn_ido": 46.9880220633556,
"tn_im1name": "144377_1.jpg",
"tn_im2name": "144377_2.jpg",
"tn_im3name": "144377_3.jpg",
"tn_im4name": "144377_4.jpg",
"type": 0,
"nico_recommended": 0
}
]
"""
现在像这样解析响应:
let jsonData = responseData.data(using: .utf8)!
let decodedItems: [MyStruct] = try! JSONDecoder().decode([MyStruct].self, from: jsonData)
decodedItems.forEach({print(">>> \([=12=])")})
实际上,您最好避免使用 force-unwrapping 数据和解码项。
输出:
>>> MyStruct(tn_newsid: 1477, tn_midashi: "知床半島とオホーツク海の絶景が広がる", tn_name: "北海道立オホーツク公園オートキャンプ場てんとらんど", tn_ktel: "0152-45-27", tn_url: "", tn_kdo: 144.239526856306, tn_ido: 43.9880220633556, tn_im1name: "144377_1.jpg", tn_im2name: "144377_2.jpg", tn_im3name: "144377_3.jpg", tn_im4name: "144377_4.jpg", type: 0, nico_recommended: 2)
>>> MyStruct(tn_newsid: 144377, tn_midashi: "知ホーツク海の絶景が広がる", tn_name: "北海道立オホートキャンプ場てんとらんど", tn_ktel: "0152-45-27", tn_url: "", tn_kdo: 144.239526856306, tn_ido: 46.9880220633556, tn_im1name: "144377_1.jpg", tn_im2name: "144377_2.jpg", tn_im3name: "144377_3.jpg", tn_im4name: "144377_4.jpg", type: 0, nico_recommended: 0)
我有一个 json file
看起来像这样:
[
{
"tn_newsid": 144377,
"tn_midashi": "知床半島とオホーツク海の絶景が広がる",
"tn_name": "北海道立オホーツク公園オートキャンプ場てんとらんど",
"tn_ktel": "0152-45-27",
"tn_url": "",
"tn_kdo": 144.239526856306,
"tn_ido": 43.9880220633556,
"tn_im1name": "144377_1.jpg",
"tn_im2name": "144377_2.jpg",
"tn_im3name": "144377_3.jpg",
"tn_im4name": "144377_4.jpg",
"type": 0,
"nico_recommended": 0
},
{
// etc ....
直到现在我都是通过截取这段代码获取数据
seasonInfoService.seasonServerDataJson(jsoncompleted: { () in
let responseData: Data = self.seasonInfoService.UpdateJsonString.data(using: String.Encoding.utf8)!
do{
let items = try JSONSerialization.jsonObject(with: responseData) as! Dictionary<String,Any>
但是 json file
以 [
开头,所以我需要将 Dictionary<String,Any>
更改为 NSArray
因为我收到这个错误代码
Could not cast value of type '__NSArrayI' (0x7fff86cd0128) to 'NSDictionary'
我的代码现在看起来像
facilitiesService.facilitiesServerDataJson(urlToFetch: url_, jsoncompleted: { () in
let responseData: Data = self.facilitiesService.UpdateJsonString.data(using: String.Encoding.utf8)!
do{
let items = try JSONSerialization.jsonObject(with: responseData) as! NSArray
print(items)
但是打印语句只显示 [
,请问如何将其更改为正确的词典列表?
首先创建一个符合Decodable的结构体。 good/necessary映射JSON数据
struct MyStruct: Decodable {
let tn_newsid: Int
let tn_midashi: String
let tn_name: String
let tn_ktel: String
let tn_url: String
let tn_kdo: Double
let tn_ido: Double
let tn_im1name: String
let tn_im2name: String
let tn_im3name: String
let tn_im4name: String
let type: Int
let nico_recommended: Int
}
那么假设您的 JSON 回复如下所示:
let responseData: String = """
[
{
"tn_newsid": 1477,
"tn_midashi": "知床半島とオホーツク海の絶景が広がる",
"tn_name": "北海道立オホーツク公園オートキャンプ場てんとらんど",
"tn_ktel": "0152-45-27",
"tn_url": "",
"tn_kdo": 144.239526856306,
"tn_ido": 43.9880220633556,
"tn_im1name": "144377_1.jpg",
"tn_im2name": "144377_2.jpg",
"tn_im3name": "144377_3.jpg",
"tn_im4name": "144377_4.jpg",
"type": 0,
"nico_recommended": 2
},
{
"tn_newsid": 144377,
"tn_midashi": "知ホーツク海の絶景が広がる",
"tn_name": "北海道立オホートキャンプ場てんとらんど",
"tn_ktel": "0152-45-27",
"tn_url": "",
"tn_kdo": 144.239526856306,
"tn_ido": 46.9880220633556,
"tn_im1name": "144377_1.jpg",
"tn_im2name": "144377_2.jpg",
"tn_im3name": "144377_3.jpg",
"tn_im4name": "144377_4.jpg",
"type": 0,
"nico_recommended": 0
}
]
"""
现在像这样解析响应:
let jsonData = responseData.data(using: .utf8)!
let decodedItems: [MyStruct] = try! JSONDecoder().decode([MyStruct].self, from: jsonData)
decodedItems.forEach({print(">>> \([=12=])")})
实际上,您最好避免使用 force-unwrapping 数据和解码项。
输出:
>>> MyStruct(tn_newsid: 1477, tn_midashi: "知床半島とオホーツク海の絶景が広がる", tn_name: "北海道立オホーツク公園オートキャンプ場てんとらんど", tn_ktel: "0152-45-27", tn_url: "", tn_kdo: 144.239526856306, tn_ido: 43.9880220633556, tn_im1name: "144377_1.jpg", tn_im2name: "144377_2.jpg", tn_im3name: "144377_3.jpg", tn_im4name: "144377_4.jpg", type: 0, nico_recommended: 2)
>>> MyStruct(tn_newsid: 144377, tn_midashi: "知ホーツク海の絶景が広がる", tn_name: "北海道立オホートキャンプ場てんとらんど", tn_ktel: "0152-45-27", tn_url: "", tn_kdo: 144.239526856306, tn_ido: 46.9880220633556, tn_im1name: "144377_1.jpg", tn_im2name: "144377_2.jpg", tn_im3name: "144377_3.jpg", tn_im4name: "144377_4.jpg", type: 0, nico_recommended: 0)