please help get data from json in swift - json

I parsed data from a json file, but I don't know how to get these variables.
Need charcode, name and value.
I need to display them in a table using swiftui. I got a mess in the console and I don't know how to get to this data
this is struct
import Foundation
struct CurrencyModel: Codable {
let valute: [String : Valute]
enum CodingKeys: String, CodingKey {
case valute = "Valute"
}
}
struct Valute: Codable {
let charCode, name: String
let value: Double
enum CodingKeys: String, CodingKey {
case charCode = "CharCode"
case name = "Name"
case value = "Value"
}
}
and this is parser
class FetchDataVM: ObservableObject {
var valueData = [String : Any]()
init() {
fetchCurrency()
}
func fetchCurrency() {
let urlString = "https://www.cbr-xml-daily.ru/daily_json.js"
let url = URL(string: urlString)
URLSession.shared.dataTask(with: url!) {data, _, error in
DispatchQueue.main.async {
if let data = data {
do {
let decoder = JSONDecoder()
let decodedData = try decoder.decode(CurrencyModel.self, from: data)
print(decodedData)
} catch {
print("Error! Something went wrong.")
}
}
}
}.resume()
}
}

As all needed information is in the Valute struct you need only the values of the valute dictionary. Replace
var valueData = [String : Any]()
with
#Published var valueData = [Valute]()
and after the line print(decodedData) insert
self.valueData = decodedData.valute.values.sorted{$0.name < $1.name}
or
self.valueData = decodedData.valute.values.sorted{$0.charCode < $1.charCode}
In the view you can iterate the array simply with a ForEach expression

Related

Unable to print specific nested json data with JSONSerialization

I'm making an api call to get some data, the json dataAsString looks like this...
guard let dataAsString = String(data: data, encoding: .utf8)else {return}
print(dataAsString)
JSON DATA
{"patch_report":{"name":"macOS Updates","patch_software_title_id":"1","total_computers":"5","total_versions":"1","versions":{"version":{"software_version":"10.15.3","computers":{"size":"5","computer":{"id":"467","name":"EPART1BGF8J9"}}}}}}
do {
// make sure this JSON is in the format we expect
if let json = try JSONSerialization.jsonObject(with:data, options:[]) as? [String: Any] {
// try to read out a string array
if let patch_report = json["patch_report"] as? [String] {
print(patch_report)
}
}
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
}
How could I use JSONSerialization to get only certain nested JSON values?
I've been told by many not to use JSONSerialization so I've provided an alternate solution. Below has the Codable solution as well.
Click Here For An Explination - Using Codable with nested JSON!
I was able to find the answers below...
Get nested JSON using JSONSerialization
do {
// make sure this JSON is in the format we expect
if let json = try JSONSerialization.jsonObject(with:data, options:[]) as? [String: Any] {
// try to read out a string array
if let patch_report = json["patch_report"] as? [String:Any] {
if let versions = patch_report["versions"] as? [String:Any] {
if let version = versions["version"] as? [String:Any] {
if let software_version = version["software_version"] as? String {
print(software_version)
}
}}
} else {print("Not Available")}
}
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
}
Get nested Json using Decoder
struct PatchReport: Codable {
var patch_report:Patch_report?
enum CodingKeys: String, CodingKey{
case patch_report = "patch_report"
}
struct Patch_report: Codable {
var name:String?
var patch_software_title_id:String?
var total_computers:String?
var total_versions:String?
var versions: Versions?
enum CodingKeys: String, CodingKey {
case name = "name"
case patch_software_title_id = "patch_software_title_id"
case total_computers = "total_computers"
case total_versions = "total_versions"
case versions = "versions"
}
struct Versions: Codable {
var version: Version?
enum CodingKeys: String, CodingKey {
case version = "version"
}
struct Version: Codable {
var software_version:String?
var computers:Computers?
enum CodingKeys: String, CodingKey {
case software_version = "software_version"
case computers = "computers"
}
struct Computers: Codable{
var size:String?
var computer:Computer?
enum CodingKeys: String, CodingKey {
case size = "size"
case computer = "computer"
}
struct Computer: Codable{
var id:String?
var name:String?
enum CodingKeys: String, CodingKey {
case id = "id"
case name = "name"
}
}
}
}
}
}
}
let response = try! JSONDecoder().decode(PatchReport.self, from: data)
print(String(response.patch_report?.total_computers ?? ""))
You're trying to cast patch_report as a String which won't work. Try this instead:
if let patch_report = json["patch_report"] as? [String: Any] {
print(patch_report["name"])
}

How to read local JSON file and output JSON in swift?

import Foundation
class ReadLocalJSON {
static func readJSONFromFile(fileName: String) -> JSON
{
var json: JSON
if let path = Bundle.main.path(forResource: fileName, ofType: "json") {
do {
let fileUrl = URL(fileURLWithPath: path)
let data = try Data(contentsOf: fileUrl, options: .mappedIfSafe)
json = try? JSONSerialization.jsonObject(with: data)
} catch {
print("Something goes wrong when reading local json file.")
}
}
return json
}
}
I try to read the local json file and output json. But the line json = try? JSONSerialization.jsonObject(with: data) gives an error saying Cannot assign value of type 'Any?' to type 'JSON'.
My json data looks like
{
"leagues":
[
{ "name": "Hockey",
"image": "hockey",
"games":
[
{
"game_state": "Final",
"game_time": 1456662600,
"home_team_city": "Alberta",
"home_team_name": "Pigs",
"home_team_score": 1,
"home_team_logo": "pig",
"visit_team_city": "Montreal",
"visit_team_name": "Fishes",
"visit_team_score": 4,
"visit_team_logo": "fish"
}
]
}
]
}
When I change the output type to be Any? I print the output and it seems missing some elements.
{
leagues = (
{
games = (
{
"game_state" = Final;
"game_time" = 1456662600;
...
How can I fix it?
Check the solution below, I used Codable for the JSON decoding.
import Foundation
struct Sports: Codable {
let leagues: [League]
}
struct League: Codable {
let name, image: String
let games: [Game]
}
struct Game: Codable {
let gameState: String
let gameTime: Int
let homeTeamCity, homeTeamName: String
let homeTeamScore: Int
let homeTeamLogo, visitTeamCity, visitTeamName: String
let visitTeamScore: Int
let visitTeamLogo: String
enum CodingKeys: String, CodingKey {
case gameState = "game_state"
case gameTime = "game_time"
case homeTeamCity = "home_team_city"
case homeTeamName = "home_team_name"
case homeTeamScore = "home_team_score"
case homeTeamLogo = "home_team_logo"
case visitTeamCity = "visit_team_city"
case visitTeamName = "visit_team_name"
case visitTeamScore = "visit_team_score"
case visitTeamLogo = "visit_team_logo"
}
}
class ReadLocalJSON {
static func readJSONFromFile(fileName: String) -> Sports?
{
let path = Bundle.main.path(forResource: fileName, ofType: "json")
let url = URL(fileURLWithPath: path!)
let sportsData = try? Data(contentsOf: url)
guard
let data = sportsData
else { return nil }
do {
let result = try JSONDecoder().decode(Sports.self, from: data)
print(result)
return result
} catch let error {
print("Failed to Decode Object", error)
return nil
}
}
}
ReadLocalJSON.readJSONFromFile(fileName: "test")
Step 1:- first make a modal class in your project
struct Welcome: Codable {
let leagues: [League]?
}
// MARK: - League
struct League: Codable {
let name, image: String?
let games: [Game]?
}
// MARK: - Game
struct Game: Codable {
let gameState: String?
let gameTime: Int?
let homeTeamCity, homeTeamName: String?
let homeTeamScore: Int?
let homeTeamLogo, visitTeamCity, visitTeamName: String?
let visitTeamScore: Int?
let visitTeamLogo: String?
enum CodingKeys: String, CodingKey {
case gameState = "game_state"
case gameTime = "game_time"
case homeTeamCity = "home_team_city"
case homeTeamName = "home_team_name"
case homeTeamScore = "home_team_score"
case homeTeamLogo = "home_team_logo"
case visitTeamCity = "visit_team_city"
case visitTeamName = "visit_team_name"
case visitTeamScore = "visit_team_score"
case visitTeamLogo = "visit_team_logo"
}
}
Step 2 : - After getting response write this line,
let decoder = JSONDecoder()
let obj = try! decoder.decode(Welcome.self, from: jsonData!)
IF you have still problem let me know

How to detect if data value is nil, when struct properties are optional

Parsing data using Codable don't fail.
when I use generic I want the parsing to fail if the fields are deferent then my object fields
struct someStruct: Codable {
var name: String?
var age: Int?
}
JSON :
{
"some_key": 123
}
You should use Codable to parse data whether it is nil or not. And after that you can check Struct like below for nil value-
if let name = yourStruct.name as? String {
} else {
//nil
}
You can just use Codable with your struct and when you parse the JSON data, and since your properties in your struct are optional, you can safe unwrap the values using if-let . I Have provided a sample example for the same
import Foundation
let jsonData = """
{
"some_key": 123
}
"""
let data = Data(jsonData.utf8)
struct someStruct: Codable {
var name: String?
var age: Int?
}
let decoder = JSONDecoder()
do {
let decodedData = try decoder.decode(someStruct.self, from: data)
// Either use this
print(decodedData.name ?? "Name not specified")
// Or use this
if let name = decodedData.name {
// Name is not nil
// Sample Example
print(name)
}
else {
// Name is nil , Handle the situation accordingly
// Sample Example
print("Name not specified")
}
} catch {
print("Could not parse JSON Data")
}
So you want to throw if a field is missing, but continue when the field is specified but nil.
You need to implement custom encoding to solve that:
enum EncodingError: Error {
case missing
}
struct Struct: Codable {
let age: Int
let name: String?
enum CodingKeys: CodingKey {
case age
case name
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
// normal decoding
age = try container.decode(Int.self, forKey: .age)
// check if key exists or throw
guard container.contains(.name) else {
throw EncodingError.missing
}
name = try container.decode(String?.self, forKey: .name)
}
}
let correctData = """
{
"age": 34,
"name": null
}
""".data(using: .utf8)!
let failData = """
{
"age": 33
}
""".data(using: .utf8)!
do {
let decoder = JSONDecoder()
let encoded = try decoder.decode(Struct.self, from: correctData) // succeeds
let fail = try decoder.decode(Struct.self, from: failData) // fails
} catch(let error) where error is EncodingError {
error
} catch {
error
}

How to filter invalid data Using swift 4.1 codable ( json decode)

I got to know struct "Codable" in swift 4.0, *.
So, I tried that when decode josn.
if let jsonData = jsonString.data(using: .utf8) {
let decodingData = try? JSONDecoder().decode(SampleModel.self, from: jsonData)
}
Example sample data model below.
struct SampleModel : Codable {
var no: Int?
var category: Int?
var template_seq: Int?
}
And sample json data is .. below.
{
"data": {
"result" : 1
"total_count": 523,
"list": [
{
"no": 16398,
"category" : 23,
"template_seq" : 1
},
{
"no": -1,
"category" : 23,
"template_seq" : 1
}
]
}
}
But i want filtering wrong data.
If the value of "no" is less than or equal to 0, it is an invalid value.
Before not using codable...below.
(using Alamifre ison response )
guard let dictionaryData = responseJSON as? [String : Any] else { return nil }
guard let resultCode = dictionaryData["result"] as? Bool , resultCode == true else { return nil }
guard let theContainedData = dictionaryData["data"] as? [String:Any] else { return nil }
guard let sampleListData = theContainedData["list"] as? [[String : Any]] else { return nil }
var myListData = [MyEstimateListData]()
for theSample in sampleListData {
guard let existNo = theSample["no"] as? Int, existNo > 0 else {
continue
}
myListData.append( ... )
}
return myListData
how to filter wrong data or invalid data using swift 4.0 Codable ??
you can make codable for inital resonse
Here is your model:
import Foundation
struct Initial: Codable {
let data: DataModel?
}
struct DataModel: Codable {
let result, totalCount: Int
let list: [List]?
enum CodingKeys: String, CodingKey {
case result
case totalCount = "total_count"
case list
}
}
struct List: Codable {
let no, category, templateSeq: Int
enum CodingKeys: String, CodingKey {
case no, category
case templateSeq = "template_seq"
}
}
extension Initial {
init(data: Data) throws {
self = try JSONDecoder().decode(Initial.self, from: data)
}
}
And use it like that :
if let initail = try? Initial.init(data: data) , let list = initail.data?.list {
var myListData = list.filter { $0.no > 0 }
}
Yes, you have to use filters for that with codable:
1: Your struct should be according to your response like that:
struct SampleModel : Codable {
var result: Int?
var total_count: Int?
var list: [List]?
}
struct List : Codable {
var no: Int?
var category: Int?
var template_seq: Int?
}
2: Parse your response using a codable struct like that:
do {
let jsonData = try JSONSerialization.data(withJSONObject: dictionaryData["data"] as Any, options: JSONSerialization.WritingOptions.prettyPrinted)
let resultData = try JSONDecoder().decode(SampleModel.self, from: jsonData)
success(result as AnyObject)
} catch let message {
print("JSON serialization error:" + "\(message)")
}
3: now you can filter invalid data simply:
let filterListData = resultData.list?.filter({$0.no > 0})
let invalidData = resultData.list?.filter({$0.no <= 0})

getting JSON Data with Swift 4 and Xcode 9

I've been trying to work with the below code to get my JSON data. It returns "Error after loading". I am using this JSON data in another application and it works. I'm trying to implement the new simplified method using Swift 4. The code does work to the point of the print statement "downloaded".
class MortgageRatesVC: UIViewController {
final let url = URL (string:"http://mortgous.com/JSON/currentRatesJSON.php")
override func viewDidLoad() {
super.viewDidLoad()
downloadJason()
}
func downloadJason () {
guard let downloadURL = url else { return }
URLSession.shared.dataTask(with: downloadURL) { data, urlResponse, error in
guard let data = data, error == nil, urlResponse != nil else {
print("Oops Call for Help")
return
}
print("downloaded")
do
{
let decoder = JSONDecoder()
let rates = try decoder.decode([LenederRates].self, from: data)
print(rates)
} catch {
print("Error after loading")
}
}.resume()
}
}
Class
class LenederRates: Codable {
let key : String
let financial_institution : String
let variable_rate : String
let six_months : String
let one_year : String
let two_year : String
let three_year : String
let four_year : String
let five_year : String
// let date : Date
init(key: String, financial_institution: String, variable_rate: String, six_months: String, one_year: String, two_year: String, three_year: String, four_year: String, five_year: String) {
self.key = key
self.financial_institution = financial_institution
self.variable_rate = variable_rate
self.six_months = six_months
self.one_year = one_year
self.two_year = two_year
self.three_year = three_year
self.four_year = four_year
self.five_year = five_year
}
}
The problem is the missing property date in your Codable class. You need to set the decoder dateDecodingStrategy to .formatted and pass a fixed format dateFormatter. Btw I suggest changing your class for a struct, change your property names using the Swift naming convention camelCase and provide the custom CodingKeys:
struct LenederRates: Codable {
let key: String
let financialInstitution : String
let variableRate: String
let sixMonths: String
let oneYear: String
let twoYear: String
let threeYear: String
let fourYear: String
let fiveYear: String
let date: Date
private enum CodingKeys: String, CodingKey {
case key, financialInstitution = "financial_institution", variableRate = "variable_rate", sixMonths = "six_months", oneYear = "one_year", twoYear = "two_year", threeYear = "three_year", fourYear = "four_year", fiveYear = "five_year", date
}
}
let mortgousURL = URL(string:"http://mortgous.com/JSON/currentRatesJSON.php")!
URLSession.shared.dataTask(with: mortgousURL) { data, urlResponse, error in
guard let data = data else { return }
do {
let dateFormat = DateFormatter()
dateFormat.locale = Locale(identifier: "en_US_POSIX")
dateFormat.dateFormat = "yyyy-MM-dd"
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .formatted(dateFormat)
let rates = try decoder.decode([LenederRates].self, from: data)
print(rates)
} catch {
print(error)
}
}.resume()