Swift, Loop through JSON returning Null -
i trying loop through list of bands have in json file (using swifty-json), reason when down looping through names, returns null.
json
small snippet of json
[ { "band":{ "id":"1", "name":"the kooks" } }, { "band":{ "id":"2", "name":"the killers" } } ]
swift code
(_, value) in json { (_,band) in value["band"] { (_,bandname) in band["name"] { print("band name: \(bandname)") } } }
the above code returns:
band name: null
band name: null
band name: null
band name: null
when try this:
for (_, value) in json { (_,brand) in value["band"] { print(band) } }
i result:
the kooks
1
the killers
2
can tell me issue is?
since value associated key "name" simple string, want use:
for (_, value) in json { (_,band) in value["band"] { if let bandname = band["name"].string { print("band name: \(bandname)") } else { print("no name specified") } } }
Comments
Post a Comment