-1

here is the string(my whole response is string) which i get after api call .

"{'result': {'ip':'49.36.183.40','id':'T1199','Date':'2022-7-24','Time':'20:58:36','Temp':38.94,'PM25':117.00,'lux':7.00,'VOC':586.00,'CO':0.97,'CO2':828.00,'O3':118.00,'RH':48.88,'Pres':989.00}}",,,

I tried this since I was receiving the string back :

@Published var singleData : [iotData] = []
    func loadSingleData() {
        
        if let url = URL(string: urlString) {
            
            
            URLSession.shared.dataTask(with: url) { [weak self ] data , response, error in
                
                
                
                DispatchQueue.main.async {
                    let decoder = JSONDecoder()
                    if let data = data {
                        if let users = try? decoder.decode([String : iotData].self, from: data) {
                            print(users)
                        } else {
                            print("failed to decode the data")
                        }
                    } else {
                        print("Failed to load the data")
                    }
                    
                }
            }.resume()
        } else {
            print("wrong url")
        }
    }

but my output is failed to decode the data

also there are commas outside json string how do i take care of those . i am beginner in ios development so i don't have any clue how to decode this json string and use it.how can i get past this

here is my model for this

struct iotData : Codable {
    
    var result : Result
}

struct Result: Codable {
    let ip, id, Date, Time: String
    let Temp: Double
    let PM25, lux, VOC: Int
    let CO: Double
    let CO2, O3: Int
    let RH: Double
    let Pres: Int

}
Karan D
  • 3
  • 2
  • 1
    Does this answer your question? [Decode using JSONDecoder Swift](https://stackoverflow.com/questions/54734856/decode-using-jsondecoder-swift) – Timmy Jul 25 '22 at 07:36
  • 3
    NEVER use `try?` with a question mark if you can't debug it. Use a proper do/try/catch. And print the thrown error. Else, it's like seeing a "Empty Gaz warning on your car", ignoring it, and asking why your car doesn't start... You just ignore the error... – Larme Jul 25 '22 at 07:54
  • 1
    Looks like a question that was asked yesterday, you need to replace single quotes with double quotes in your json first. If you have trailing commas you need to remove them as well. Overall this looks like the returned data is of very poor quality – Joakim Danielson Jul 25 '22 at 07:58
  • @JoakimDanielson atleast tell me how record this response inside variable as string without decoding it – Karan D Jul 25 '22 at 08:37
  • If you use a proper do/try/catch, you should get an error saying that the JSON isn't valid. And, you can test it in a JSON validator (there are plenty online). So, where are you getting that? is it supposed to be JSON? As long as it's not JSON, how are you expecting `JSONDecoder` to work? Currently, your starting point "Decoding the incoming json string" is false. is your API really supposed to send back JSON? Do you need "application/json" as content type to do so? it's not done in your code. – Larme Jul 25 '22 at 08:44
  • Use `String(data: data, encoding: .utf8)`, see https://developer.apple.com/documentation/swift/string/init(data:encoding:) – Joakim Danielson Jul 25 '22 at 09:28

1 Answers1

1

Use:

decoder.decode(iotData.self, from: data) { }

Instead of

decoder.decode(iotData, from: data) { }

Also make sure 'data' is actually 'Data' object

CloudBalancing
  • 1,461
  • 2
  • 11
  • 22