Swift Parsing with Json - json

I'm trying to parsing Json using swift and this is the json I get
{"Data":{"KanBan":[{"Sdate":"2020/06/22","Stype":"上班卡","Stime":"09:31:11","Ip":"xxx.xxx.xxx.xx"},{"Sdate":"2020/06/20","Stype":"下班卡","Stime":"20:53:43","Ip":"xxx.xxx.xxx.xx"},{"Sdate":"2020/06/20","Stype":"下班卡","Stime":"20:48:25","Ip":"xxx.xxx.xxx.xx"},{"Sdate":"2020/06/20","Stype":"下班卡","Stime":"18:53:57","Ip":"xxx.xxx.xxx.xx"},{"Sdate":"2020/06/20","Stype":"上班卡","Stime":"18:43:54","Ip":"xxx.xxx.xxx.xx "}]},"IsSuccess":true,"Message":null,"ErrorCode":null,"ErrorMessage":null}
Below is the struct I defined and use it with Swift JsonDecoder :
// inside viewController
let decoder = JSONDecoder()
let historys = try decoder.decode([Datas].self, from: data)
// struct I defined myself
struct Datas : Codable {
let Data: KanBan
let IsSuccess: Bool
let Message: String
let ErrorCode: String
let ErrorMessage: String
init( Data: KanBan, IsSuccess: Bool, Message: String, ErrorCode: String, ErrorMessage: String) {
self.Data = Data
self.IsSuccess = IsSuccess
self.Message = Message
self.ErrorCode = ErrorCode
self.ErrorMessage = ErrorMessage
}
}
struct KanBan : Codable {
let KanBan: [details]
init( kanbans : [details]) {
self.KanBan = kanbans
}
}
struct details: Codable {
let Sdate: Date
let Stype: String
let Stime : String
let Ip: String
init(Sdate: Date, Stype: String, Stime : String, Ip: String ) {
self.Sdate = Sdate
self.Stype = Stype
self.Stime = Stime
self.Ip = Ip
}
}
I keep get error that said parsing failed .
Whats wrong with the struct I defined ? Plz help and thanks!!!

You need to fix 3 things:
You're trying to decode [Datas] whereas it should be Datas.
If the keys are optional you should declare them as optional. eg: ErrorMessage, ErrorCode.
Provide the DateFormatter if you're trying to decode Dates.
Here's the code you need: -
Model:
struct Datas: Codable {
let Data: KanBan
let IsSuccess: Bool
let Message: String?
let ErrorCode: String?
let ErrorMessage: String?
}
struct KanBan: Codable {
let KanBan: [details]
}
struct details: Codable {
let Sdate: Date
let Stype: String
let Stime : String
let Ip: String
}
Decoding:
do {
let decoder = JSONDecoder()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy/MM/dd"
decoder.dateDecodingStrategy = .formatted(dateFormatter)
let historys = try decoder.decode(Datas.self, from: data)
print(historys)
} catch { print(error) }

Related

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

Deserialization JSON swift 4.2

I try to deserialize my JSON by using Decodable protocol, also i use enum with CodingKey, but it doesn't work. I need only nested array (start with "indicator"), and only few fields (all of them in struct). I tried a lot of different options, but unfortunately..
P.S. Also i tried to do it without CodingKey. Anyway response was: "Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "country", intValue: nil)" Ofc i read this, maybe array is a reason(i mean this strange intValue)?
JSON
[
{
"page":1,
"pages":2,
"per_page":50,
"total":59,
"sourceid":"2",
"lastupdated":"2019-03-21"
},
[
{
"indicator":{
"id":"IP.PAT.RESD",
"value":"Patent applications, residents"
},
"country":{
"id":"SS",
"value":"South Sudan"
},
"countryiso3code":"SSD",
"date":"2018",
"value":null,
"unit":"",
"obs_status":"",
"decimal":0
},
{
"indicator":{
"id":"IP.PAT.RESD",
"value":"Patent applications, residents"
},
"country":{
"id":"SS",
"value":"South Sudan"
},
"countryiso3code":"SSD",
"date":"2017",
"value":null,
"unit":"",
"obs_status":"",
"decimal":0
},
...
]
]
My code
struct CountryObject: Decodable{
var country: CountryInfo
var date: Int
var value: Int?
private enum RawValues: String, Decodable{
case date = "date"
case vallue = "value"
}
}
struct CountryInfo: Decodable{//Country names
var id: String?
var value: String?
private enum RawValues: String, Decodable{
case id = "id"
case value = "value"
}
}//
let urlString = "https://api.worldbank.org/v2/country/SS/indicator/IP.PAT.RESD?format=json"
guard let url = URL(string: urlString) else {return}
URLSession.shared.dataTask(with: url) {(data,response,error) in
guard let data = data else {return}
guard error == nil else {return}
do{
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let countryObject = try! decoder.decode([CountryObject].self, from: data)
print(countryObject)
}catch let error{
print(error)
}
}.resume()
Create a root struct and decode the array with unkeyedContainer
struct Root : Decodable {
let info : Info
let countryObjects : [CountryObject]
init(from decoder: Decoder) throws {
var arrayContrainer = try decoder.unkeyedContainer()
info = try arrayContrainer.decode(Info.self)
countryObject = try arrayContrainer.decode([CountryObject].self)
}
}
struct Info : Decodable {
let page, pages, perPage: Int
let lastupdated: String
}
struct CountryObject : Decodable {
let country: CountryInfo
let date: String
let value: Int?
}
struct CountryInfo : Decodable { //Country names
let id: String
let value: String
}
...
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
do {
let root = try decoder.decode(Root.self, from: data)
let countryObjects = root.countryObjects
print(countryObjects)
} catch { print(error) }
(De)serializing the JSON twice is unnecessarily expensive.

SWIFT 4, Xcode 9, JSON DECODER

I'm pretty stuck now. I'm attempting to PARS a JSON RETURN for just the year make make and model. It's buried in an array of dictionaries, and the decoder is having a hard time pulling them out. What am I doing wrong?
public struct Page: Decodable {
let Count: Int
let Message: String
let SearchCriteria: String
let Results: [car]}
public struct car: Decodable {
let ModelYear: String
let Make: String
let Model: String
let VIN: String}
let session = URLSession.shared
let components = NSURLComponents()
components.scheme = "https"
components.host = "vpic.nhtsa.dot.gov"
components.path = "/api/vehicles/decodevinvaluesextended/\(VIN)"
components.queryItems = [NSURLQueryItem]() as [URLQueryItem]
let queryItem1 = NSURLQueryItem(name: "Format", value: "json")
components.queryItems!.append(queryItem1 as URLQueryItem)
print(components.url!)
let task = session.dataTask(with: components.url!, completionHandler: {(data, response, error) in
guard let data = data else { return }
do
{
//let Result = try JSONDecoder().decode(Page.self, from: data)
// let PageResult = try JSONDecoder().decode(Page.self, from: data)
let json = try JSONDecoder().decode(Page.self, from: data)
let Results = json.Results;
print(Results)
First of all it's highly recommended to conform to the Swift naming convention that variable names start with a lowercase and structs start with a capital letter
public struct Page: Decodable {
private enum CodingKeys : String, CodingKey {
case count = "Count", message = "Message", searchCriteria = "SearchCriteria", cars = "Results"
}
let count: Int
let message: String
let searchCriteria: String
let cars: [Car]
}
public struct Car: Decodable {
private enum CodingKeys : String, CodingKey {
case modelYear = "ModelYear", make = "Make", model = "Model", VIN
}
let modelYear: String
let make: String
let model: String
let VIN: String
}
The cars array is in the variable result. This code prints all values
let result = try JSONDecoder().decode(Page.self, from: data)
for car in result.cars {
print("Make: \(car.make), model: \(car.model), year: \(car.modelYear), VIN: \(car.VIN)")
}

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()

Parse Json with a definition at the beginning

I want to parse a Json file with this structure:
{"x":"exchange","b":"usd","ds":["exchange","avgp","mcap","ppc7D","ppc12h","ppc4h","ppc24h"],"data":[["Dow Jones","16360.447","273.89B","6.62","2.14","-0.59","-1.99"],["Dax","877.422","6.80B","38.15","-7.4","-4.44","-4.12"],["nikkei","30.077","2.96B","24.22","-2.3","-4.02","-4.95"],["ATX","281.509","15.29B","214.97","-5.48","-4.58","-10.77"]]}
I do not know how to parse a Json file where there is no definition like:
exchange = "Dow Jones", avgp = 16360.477" etc.
And i could not found anything online.
My code looks like this:
let json = """
{"x":"exchange","b":"usd","ds":["exchange","avgp","mcap","ppc7D","ppc12h","ppc4h","ppc24h"],"data":[["Dow Jones","16360.447","273.89B","6.62","2.14","-0.59","-1.99"],["Dax","877.422","6.80B","38.15","-7.4","-4.44","-4.12"],["nikkei","30.077","2.96B","24.22","-2.3","-4.02","-4.95"],["ATX","281.509","15.29B","214.97","-5.48","-4.58","-10.77"]]}
""".data(using: .utf8)!
struct JsonWebsocket: Decodable {
let exchange: String
let avgp: String
let mcap: String
let ppc7D: String
let ppc12h: String
let ppc4h: String
let ppc24h: String
init(exchange: String, avgp: String, mcap: String, ppc7D: String, ppc12h: String, ppc4h: String, ppc24h: String) {
self.exchange = exchange
self.avgp = avgp
self.mcap = mcap
self.ppc7D = ppc7D
self.ppc12h = ppc24h
self.ppc4h = ppc4h
self.ppc24h = ppc24h
}
}
func fetchJson() -> [String:JsonWebsocket] {
let jsonCoinsDecode = json
let coinDecode = JSONDecoder()
let output = try! coinDecode.decode([String:JsonWebsocket].self, from: jsonCoinsDecode)
return output
}
let array = fetchDataTradingPairs()
But of course it returns an error as the structure does not match the json file.
Does anyone know how to parse this json?
Thanks!
create a struct like that.
struct JsonWebsocket: Decodable {
let x: String
let b: String
let ds: [String]
let data: [[String]]
}
and decode
do {
let coinDecode = JSONDecoder()
let output = try coinDecode.decode(JsonWebsocket.self, from: json)
print(output.data)
}
catch let error{
print(error.localizedDescription)
}
alter native
otherwise, create a custom
struct JsonWebsocket: Decodable {
let exchange: String
let avgp: String
let mcap: String
let ppc7D: String
let ppc12h: String
let ppc4h: String
let ppc24h: String
init(json: [String]) {
self.exchange = json[0]
self.avgp = json[1]
self.mcap = json[2]
self.ppc7D = json[3]
self.ppc12h = json[4]
self.ppc4h = json[5]
self.ppc24h = json[6]
}
}
do {
let jsonData = try JSONSerialization.jsonObject(with: json, options: []) as? [String: Any]
var jsonWebsocket: [JsonWebsocket] = []
if let data = jsonData!["data"] as? [[String]] {
for d in data {
jsonWebsocket.append(JsonWebsocket(json: d))
}
}
print(jsonWebsocket.count)
}
catch let error{
print(error.localizedDescription)
}