1

I am trying to get data from JSON. So far, I can extract it and print it.

My goal now is to be able to use it in my ContentView so that it can be used in a Text View or something similar. I have tried creating @State variables, passing it as a parameter, etc. etc. and nothing seems to work.

I'm fairly new to SwiftUI, so I appreciate the help!

struct GeoService: Codable {
    var status: String
    var results: [GeoResult]
}

struct GeoResult: Codable {
    
    struct Geometry: Codable {
        
        struct Location: Codable {
            
            let lat: Float
            let lng: Float
            
            init() {
                lat = 32
                lng = 30
            }
        }
        let location: Location
    }
    let formatted_address: String
    let geometry: Geometry
}



struct ContentView: View {

//    @State private var results: Any ?????????
    
    var body: some View {
        NavigationView {
            Text("Test")
                .navigationTitle("Quotes")
                .task {
                    await handleData()
                }
        }
        
    }
    
    func handleData() async {
        let geoResult="""
        {
          "results": [
            {
              "formatted_address": "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
              "geometry": {
                "location": {
                  "lat": 37.4224764,
                  "lng": -122.0842499
                }
              }
            },
            {
              "formatted_address": "Test addresss",
              "geometry": {
                "location": {
                  "lat": 120.32132145,
                  "lng": -43.90235469
                }
              }
            }
          ],
          "status": "OK"
        }
        """.data(using: .utf8)!
        
        let decoder = JSONDecoder()
        print("executing handleData()")
        do {
            let obj = try decoder.decode(GeoService.self, from: geoResult)
            for result in obj.results {
                print("Address: \(result.formatted_address)")
                print("Lat/long: (\(result.geometry.location.lat), \(result.geometry.location.lng))")
            }
        } catch {
            print("Did not work :(")
        }
    }
}
5
  • Does this answer your question? SwiftUi Decoding JSON Commented May 19, 2022 at 1:41
  • @Yrb not quite - I'm trying to create a variable within ContentView. I want to set the variable's value in handleData() so that I can use it within a Text() or something similar Commented May 19, 2022 at 1:56
  • Your @State should be an array of [GeoResult]. Eventually, you should investigate into using MVVM pattern. youtu.be/wfW0lhr_88I Commented May 19, 2022 at 3:11
  • 1
    @azamsharp thank you! This is actually just a little test project I have that I plan to integrate into a weather app. Commented May 19, 2022 at 3:19
  • As a follow up - how would I initialize the @State variable? Commented May 19, 2022 at 3:22

1 Answer 1

2

I moved the get request to its own class so we can leverage ObservableObject and Publish the data. Publishing the data allows the subscribing views to update data when the published data changes. In this case, your ContentView is subscribing to the data via the @ObservedObject var geoData: ResponseData line of code.

Additionally, I added how to access two pieces of relevant data in your view, and displayed it in a list for easy reading. This should give you an idea of how to access/display the data.

Hopefully this provides enough information for you to tweak the answer to get it work the way you desire.

import SwiftUI

struct GeoService: Codable {
    var status: String?
    var results: [GeoResult]?
}

struct GeoResult: Codable {

 struct Geometry: Codable {
    
    struct Location: Codable {
        
        let lat: Float
        let lng: Float
        
        init() {
            lat = 32
            lng = 30
        }
    }
    let location: Location
}

let formatted_address: String
    let geometry: Geometry
}

struct ContentView: View {
 @ObservedObject var geoData: ResponseData

 var body: some View {
    NavigationView {
        if #available(iOS 15.0, *) {
            List {
                Text(geoData.geoResultsData?.results?[0].formatted_address ?? "Loading")
                Text(String(geoData.geoResultsData?.results?[0].geometry.location.lat ?? 0))
            }
            .navigationTitle("Quotes")
            .task {
                await geoData.handleData()
                print(geoData.geoResultsData, "yessssss")
            }
        } else {
            Text("failure")
        }
    }
  }
}

class ResponseData: ObservableObject {

@Published var geoResultsData: GeoService?

func handleData() async {
    let geoResult="""
    {
      "results": [
        {
          "formatted_address": "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
          "geometry": {
            "location": {
              "lat": 37.4224764,
              "lng": -122.0842499
            }
          }
        },
        {
          "formatted_address": "Test addresss",
          "geometry": {
            "location": {
              "lat": 120.32132145,
              "lng": -43.90235469
            }
          }
        }
      ],
      "status": "OK"
    }
    """.data(using: .utf8)!
    
    let decoder = JSONDecoder()
    print("executing handleData()")
    do {
        let obj = try decoder.decode(GeoService.self, from: geoResult)
        geoResultsData = obj
    } catch {
        print("Did not work :(")
    }
  }
}

EDIT:

You will need to initialize some data within your app. You can initialize it as empty for the time being, if desired. This can be done by doing something like the following: ContentView(geoData: ResponseData.init())

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you @nickreps! This works great. One question - if I wanted to loop through it, how would I do so? I have tried with ForEach and get lots of errors about the types being incorrect. My code is below: List { ForEach((responseData.geoResultsData?.results)!) { value in Text(value.formatted_address) Text(value.geometry.location.lat) } }
That is a wholly different question that should be asked as a new question. Also, if @nickreps code works as you say, please accept it as the answer.
@QuintonPrice Happy to help! I do agree with Yrb. Your comment should be asked as a new, separate question. If the solution works as expected, please be sure to accept it as the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.