I have a json file
which looks like this:
[
{
"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 ....
Untill now I was getting the data with this code snipped
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>
But the json file
starts with [
so I need to change Dictionary<String,Any>
to NSArray
because Im getting this error code
Could not cast value of type '__NSArrayI' (0x7fff86cd0128) to 'NSDictionary'
and my code now looks like
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)
But the print statement shows only [
, how can I change this to proper list of dictionaries please?
CodePudding user response:
First create a struct that conforms to Decodable. It's good/necessary to map the JSON data.
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
}
Then let's say your JSON response looks like this:
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
}
]
"""
Now parse the response like this:
let jsonData = responseData.data(using: .utf8)!
let decodedItems: [MyStruct] = try! JSONDecoder().decode([MyStruct].self, from: jsonData)
decodedItems.forEach({print(">>> \($0)")})
In practice, you'd better avoid force-unwrapping data and decoded items.
Output:
>>> 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)