A beginner here.
I'm having some trouble trying to parse this json url in SwiftUI
Here is what I've tried:
import SwiftUI
struct PriceData2: Decodable {
var id: Int
var last: Double
var lowestAsk: Double
var highestBid: Double
var percentChange: Double
var baseVolume: Double
var quoteVolume: Double
var isFrozen: Int
var high24hr: Double
var low24hr: Double
var change: Double
var prevClose: Double
var prevOpen: Double
}
struct ContentView: View {
#State var priceData2: PriceData2?
var body: some View {
if #available(iOS 15.0, *) {
VStack(){
Text("\(priceData2?.id ?? 0)")
}.onAppear (perform: loadData2)
} else {
// Fallback on earlier versions
}
}
public func loadData2() {
guard let url = URL(string: "https://api.bitkub.com/api/market/ticker?sym=THB_BTC") else {
return
}
URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data else {return}
if let decodedData = try? JSONDecoder().decode(PriceData2.self, from: data){
DispatchQueue.main.async {
self.priceData2 = decodedData
}
}
}.resume()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I think it has something to do with the THB_BTC property of the dictionary that I haven't dealt with but I'm not totally sure of.
Any help with this is much appreciated.
Thanks
What is missing when decoding is a top level type to hold the price data and there are two ways to solve this, either by using a top level struct or by using a dictionary.
Since the top level key, THB_BTC, seems to be the "symbol" argument from the query and thus might change a top level dictionary is the best alternative here
do {
let result = try JSONDecoder().decode([String: PriceData].self, from: data)
if let priceData = result.values.first { //or = result["THB_BTC"]
print(priceData)
}
} catch {
print(error)
}
Just for completeness, here is the variant with the top level struct
struct Result: Decodable {
enum CodingKeys: String, CodingKey {
case data = "THB_BTC"
}
let data: PriceData
}
let result = try JSONDecoder().decode(Result.self, from: data)
print(result.data)
Related
I am working on an app that fetches the data from JSON and displays it.
However, I am stuck with an error saying Instance method 'appendInterpolation(_:formatter:)' requires that '[String : Int]' inherit from 'NSObject'
Here is my data structure:
struct Data: Codable {
var message: String
var data: Objects
}
struct Objects: Codable {
var date: String
var day: Int
var resource: String
var stats, increase: [String: Int]
}
Function to fetch the data:
func getData() {
let urlString = "https://russianwarship.rip/api/v1/statistics/latest"
let url = URL(string: urlString)
URLSession.shared.dataTask(with: url!) { data, _, error in
if let data = data {
do {
let decoder = JSONDecoder()
let decodedData = try decoder.decode(Data.self, from: data)
self.data = decodedData
} catch {
print("Hey there's an error: \(error.localizedDescription)")
}
}
}.resume()
}
And a ContentView with the #State property to pass the placeholder data:
struct ContentView: View {
#State var data = Data(message: "", data: Objects(date: "123", day: 123, resource: "", stats: ["123" : 1], increase: ["123" : 1]))
var body: some View {
VStack {
Button("refresh") { getData() }
Text("\(data.data.date)")
Text("\(data.data.day)")
Text(data.message)
Text("\(data.data.stats)") //error is here
Here is an example of JSON response
I wonder if the problem is in data structure, because both
Text("\(data.data.date)")
Text("\(data.data.day)")
are working just fine. If there are any workarounds with this issue – please, I would highly appreciate your help!:)
stats is [String: Int], and so when you want to use it, you need to supply the key to get the value Int, the result is an optional that you must unwrap or supply a default value in Text
So use this:
Text("\(data.data.stats["123"] ?? 0)")
And as mentioned in the comments, do not use Data for your struct name.
EDIT-1: there are two ways you can make the struct fields camelCase; one is using the CodingKeys as shown in ItemModel, or at the decoding stage, as shown in the getData() function. Note, I've also updated your models to make them easier to use.
struct DataModel: Codable {
var message: String
var data: ObjectModel
}
struct ObjectModel: Codable {
var date: String
var day: Int
var resource: String
var stats: ItemModel
var increase: ItemModel
}
struct ItemModel: Codable {
var personnelUnits: Int
var tanks: Int
var armouredFightingVehicles: Int
// ...
// manual CodingKeys
// enum CodingKeys: String, CodingKey {
// case tanks
// case personnelUnits = "personnel_units"
// case armouredFightingVehicles = "armoured_fighting_vehicles"
// }
}
struct ContentView: View {
#State var dataModel = DataModel(message: "", data: ObjectModel(date: "123", day: 123, resource: "", stats: ItemModel(personnelUnits: 123, tanks: 456, armouredFightingVehicles: 789), increase: ItemModel(personnelUnits: 3, tanks: 4, armouredFightingVehicles: 5)))
var body: some View {
VStack {
Button("get data from Server") { getData() }
Text("\(dataModel.data.date)")
Text("\(dataModel.data.day)")
Text(dataModel.message)
Text("\(dataModel.data.stats.armouredFightingVehicles)") // <-- here
}
}
func getData() {
let urlString = "https://russianwarship.rip/api/v1/statistics/latest"
if let url = URL(string: urlString) {
URLSession.shared.dataTask(with: url) { data, _, error in
if let data = data {
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase // <-- here
dataModel = try decoder.decode(DataModel.self, from: data)
} catch {
print("--> error: \(error)")
}
}
}.resume()
}
}
}
I need my JSON data to be translated to a list as an output for an app I am creating.
I have found this tutorial, which seems to be using some old elements like "BindableObject":
https://www.youtube.com/watch?v=ri1A032zfLo
As far as I've checked several times, I went word for word in the NetworkingManager file being described from 1:52 in the video (just changing the names and the URL). My code:
import Foundation
import SwiftUI
import Combine
/*BindableObject was renamed to ObservableObject */
class NetworkingManager: ObservableObject{
var didChange: PassthroughSubject <NetworkingManager, Never>
var gitHubList = Root(items: []){
didSet{
didChange.send(self)
}
}
init() {
guard let url = URL(string: "https://api.github.com/search/repositories?q=CoreData&per_page=20") else { return }
URLSession.shared.dataTask(with: url) {(data, _, _) in guard let data = data else { return }
let gitHubList = try! JSONDecoder().decode(GitHubAPIlist.self, from: data)
DispatchQueue.main.async {
self.gitHubList = gitHubList
}
}.resume()
}
}
I get 2 errors:
'self' captured by a closure before all members were initialized
on the line with URLSession and
Return from initializer without initializing all stored properties
on the line with the } after the .resume() command
Are there some obsolete syntaxes in the code or am I missing something?
try something like this approach, to get you data from github. Works very well for me:
class NetworkingManager: ObservableObject{
#Published var gitHubList: [Item] = []
init() {
loadData()
}
func loadData() {
guard let url = URL(string: "https://api.github.com/search/repositories?q=CoreData&per_page=20") else { return }
URLSession.shared.dataTask(with: url) {(data, _, _) in
guard let data = data else { return }
do {
let response = try JSONDecoder().decode(Root.self, from: data)
DispatchQueue.main.async {
self.gitHubList = response.items
}
} catch {
print("error: \(error)")
}
}.resume()
}
}
struct Root: Codable {
let totalCount: Int
let incompleteResults: Bool
let items: [Item]
enum CodingKeys: String, CodingKey {
case totalCount = "total_count"
case incompleteResults = "incomplete_results"
case items
}
}
struct Item: Identifiable, Codable {
let keysURL, statusesURL, issuesURL: String
let id: Int
let url: String
let pullsURL: String
// ... more properties
enum CodingKeys: String, CodingKey {
case keysURL = "keys_url"
case statusesURL = "statuses_url"
case issuesURL = "issues_url"
case id
case url
case pullsURL = "pulls_url"
}
}
struct ContentView: View {
#StateObject var netManager = NetworkingManager()
var body: some View {
List {
ForEach(netManager.gitHubList) { item in
Text(item.url)
}
}
}
}
You declared the subject but didn't initialize it
var didChange = PassthroughSubject<NetworkingManager, Never>()
However actually you don't need the subject, mark gitHubList as #Published (and delete the subject).
And if you are only interested in the items declare
#Published var gitHubList = [Repository]()
and assign
DispatchQueue.main.async {
self.gitHubList = gitHubList.items
}
The #Published property wrapper will update the view.
In the view declare NetworkingManager as #StateObject
#StateObject private var networkManager = NetworkingManager()
and in the rendering area refer to
List(networkManager.gitHubList...
Notes:
Forget the tutorial , the video is more than two years old which are ages in terms of Swift(UI)'s evolution speed.
Ignoring errors and force unwrapping the result of the JSON decoder is very bad practice. Handle the potential URLSession error and catch and handle the potential decoding error.
I’m trying to loading the content from a JSON that I created, but unfortunately it doesn’t work.
As you can see from the code below, the only thing I see is “Error” printed in the console.
I also tried with other JSONs from the web, changing the struct accordingly, and it works, but not with the JSON I created.
Here is the code:
import SwiftUI
import URLImage
struct Busso: Codable, Identifiable {
public var id: UUID
public var titolo: String
public var autore: String
public var testo: String
public var data: String
public var extra1: String
public var extra2: String
public var foto: String
public var fotoUrl: String
}
class FetchBusso: ObservableObject {
#Published var Bussos = [Busso]()
init() {
let url = URL(string: "https://geniuspointfrezza.altervista.org/index.php?json=1")!
URLSession.shared.dataTask(with: url) {(data, response, error) in
do {
if let bussoData = data {
let decodedData = try JSONDecoder().decode([Busso].self, from: bussoData)
DispatchQueue.main.async {
self.Bussos = decodedData
}
} else {
print("No data")
}
} catch {
print("Error")
}
}.resume()
}
}
struct CategoryView: View {
#ObservedObject var fetch = FetchBusso()
var body: some View {
VStack {
List(fetch.Bussos) { todo in
VStack(alignment: .leading) {
Text(todo.titolo)
}
}
}
}
}
struct CategoryView_Previews: PreviewProvider {
static var previews: some View {
CategoryView()
}
}
And here is the link to the JSON I want to use:
https://geniuspointfrezza.altervista.org/index.php?json=1
Waiting for your advice, thank you!
Json I'm throwing the site to help you read. You can get help at the point where you get stuck.shttps: //app.quicktype.io
The issue is
public var id: UUID your JSON is public var id: String
catch {
print(error)
}
is more useful than
print("Error")
Your Busso struct is not a proper decoder for the given JSON. As I gave you the link to a prior one of my answers that explains how to get a struct that will parse your data. You really need to review that, but for the sake of putting this to bed, the struct should be:
// MARK: - GeniuspointfrezzaDecoder
struct GeniuspointfrezzaDecoder: Codable {
let id: String?
let titolo: String?
let autore: String?
let testo: String?
let data: String?
let extra1: String?
let extra2: String?
let creazione: String?
let foto: String?
let fotoURL: String?
enum CodingKeys: String, CodingKey {
case id, titolo, autore, testo, data, extra1, extra2, creazione, foto
case fotoURL = "fotoUrl"
}
}
I made the elements all optional since you don't actually know if the server response will contain all of the elements. Another issue you had was declaring the id in your decoder to be a UUID, when the JSON response is clearly a string. Changing the response data to your app's needs has to be done AFTER decoding the response. The decoder is NOT your Busso struct. It is a decoder that gives you the data to put into your Busso struct.
I am new to Swift and SwiftUI. I am currently teaching myself how to code (loving it!) through hackingwithswift.com I am currently on Day 60 and I am stuck and not sure what to do from here.
The challenge is to decode some information using Codable and populate SwiftUI.
I created a struct to match the JSON, but when I go to run the app, I keep getting my error "Fetch Failed: Unknown Error" and therefore my UI won't update.
Would someone glance at my code and provide any pointers on where I am going wrong and possibly why? Thank you so much for any suggestions and help, it is much appreciated! Code is posted below.
Cody
import SwiftUI
struct Response: Codable {
var results: [User]
}
struct User: Codable, Identifiable {
let id: String
let isActive: Bool
let name: String
let age: Int
let company: String
let email: String
let address: String
let about: String
let registered: String
let tags: [String]
struct FriendRole: Codable {
let id: String
let name: String
}
let friend: [FriendRole]
}
struct ContentView: View {
#State private var results = [User]()
var body: some View {
List(results, id: \.id) { item in
VStack(alignment: .leading) {
Text(item.name)
.font(.headline)
Text(item.address)
}
}
.onAppear(perform: loadData)
}
func loadData() {
guard let url = URL(string: "https://www.hackingwithswift.com/samples/friendface.json") else {
print("Invalid URL")
return
}
let request = URLRequest(url: url)
URLSession.shared.dataTask(with: request) { data, response, error in
if let data = data {
if let decodedResponse = try? JSONDecoder().decode(Response.self, from: data) {
DispatchQueue.main.async {
self.results = decodedResponse.results
}
return
}
}
print("Fetch Failed: \(error?.localizedDescription ?? "Unkown Error").")
}.resume()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I'm also new to swiftUI this is how I managed to make it work by using Observable Objects
Here's the structures Since in the json file there is a [ in the very beginning you do not have to create a struct that has a User array you could create a struct and then create a variable of that struct type as an array
here's how I did it
I created separate files for instance for the structures i had a different file for them
here's what I have for my structure file
import Foundation
struct User : Codable, Identifiable {
let id : String
let isActive : Bool
let name : String
let age : Int
let company : String
let email : String
let address : String
let about : String
let registered : String
let tags = [String]()
let friends = [Friends]()
}
struct Friends : Codable {
let id : String
let name : String
}
I created another file for the observable object class
class JsonChannel : ObservableObject {
#Published var retVal = [User]()
func getInfo () {
guard let url = URL(string: "https://www.hackingwithswift.com/samples/friendface.json") else {return}
URLSession.shared.dataTask(with: url) { (data, resp, err) in
if let data = data {
DispatchQueue.main.async {
do {
self.retVal = try JSONDecoder().decode([User].self, from: data)
}
catch {
print(error)
}
}
}
}.resume()
}
}
and here's what i have for my contentView file
import SwiftUI
struct ContentView : View {
#ObservedObject var info = JsonChannel()
var body: some View {
VStack {
Button(action: {
self.info.getInfo()
}) {
Text("click here to get info")
}
List {
ForEach (self.info.retVal) { item in
VStack {
Text("\(item.name)")
Text("\(item.address)")
}
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I am playing with the Instagram API and I encounter the following issue: I managed to decode and access the JSON data but when I try to access a certain value inside a SwiftUI View I encounter the following error: Thread 1: Fatal error: Index out of range. I am totally aware that this is triggered by the fact that the API load is asynchronous but I can not find a fix for this.
I would greatly appreciate your help
Below you can find the API response
Here you can find my model and JSON Decoder
struct InstaAPI: Codable {
var name: String
var period: String
var description: String
var values: [ValueResponse]
}
struct ValueResponse: Codable {
let value: String
}
struct Entry: Codable {
let data: [InstaAPI]
}
class getData: ObservableObject {
#Published var response = [Entry]()
init() {
downloadJSON(from: URL(string: "https://graph.facebook.com/v6.0/17841402116620153/insights?metric=impressions&period=day&access_token=accounttoken")!)
}
func downloadJSON(from url: URL) {
URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data {
let jsonDecoder = JSONDecoder()
do {
let parsedJson = try jsonDecoder.decode(Entry.self, from: data)
DispatchQueue.main.async {
self.response.append(parsedJson)
}
for data in parsedJson.data {
print(data.values[0].value)
}
} catch {
print(error)
}
}
}.resume()
}
}
maybe the old fashioned if would do the trick:
struct ContentView: View {
#ObservedObject var response = getData()
#State var responseNdx = 0
#State var dataNdx = 0
var body: some View {
VStack {
if responseNdx < self.response.response.count {
if dataNdx < self.response.response[responseNdx].data.count {
Text(self.response.response[responseNdx].data[dataNdx].name)
}
}
}
}
}