简体   繁体   中英

sending parsed JSON data to views

I'm stuck because I can't send JSON data from URLSession func to views. I read the 90% of previous commends and watch lots of videos but I didn't migrate to my project. Here is my code blocks that I need help ;

This ones my json struct

struct APIResponse: Decodable{
    let stocks: [Stocks]
}

struct Stocks: Decodable{
    
    let id: Int
    let difference: Float
    let bid:Float
    let isDown: Bool
    let isUp: Bool
    let offer: Float
    let price: Float
    let symbol: String
    let volume: Double

    
}

this one is mine JsonDecode code block;

if let data2 = data2 {
 do {
 // let json = try JSONSerialization.jsonObject(with: data2, options: [])
                                    
let apiResponse  = try JSONDecoder().decode(APIResponse.self, from: data2)
                                    
 print(apiResponse.stocks[2].volume)

   DispatchQueue.main.async {
       completed()
            }

        }catch{
            print(error)
              }    
         }
            }.resume()

when I watch videos about it they were [APIResponse].self but when I try that way my code is failed, in my way json parse is working (I can call like 'apiResponse.stocks[2].id') but I can't send this apiResponse data to views.

example of my JSON file

{
    status =     {
        error =         {
            code = 0;
            message = "";
        };
        isSuccess = 1;
    };
    stocks =     (
                {
            bid = "31.5";
            difference = "-0.2";
            id = 1190;
            isDown = 1;
            isUp = 0;
            offer = "31.6";
            price = "31.81";
            symbol = "P4jfFAYOTiLEih2Ic+NAkg==";
            volume = "801457.5";
        },
                {
            bid = "4.25";
            difference = "-0.04";
            id = 471;
            isDown = 1;
            isUp = 0;
            offer = "4.26";
            price = "4.31";
            symbol = "zomIgqEl79jIE+TJ7xV4yQ==";
            volume = "349264.21";
        },
                {
            bid = "2.86";
            difference = "-0.01";
            id = 472;
            isDown = 1;
            isUp = 0;
            offer = "2.87";
            price = "2.87";
            symbol = "2DlR317+autGo3fiKwNhFA==";
            volume = "19279.4";
        },
                {
            bid = 55;
            difference = 1;
            id = 473;
            isDown = 0;
            isUp = 1;
            offer = "55.25";
            price = "56.74";
            symbol = "fvo0GQ+pqUmHXwm062Gatw==";
            volume = "2647954.25";
        },       {
            bid = "1.22";
            difference = "-0.04";
            id = 465;
            isDown = 1;
            isUp = 0;
            offer = "1.23";
            price = "1.26";
            symbol = "wR/24WChHVRFWZSUW1UdwQ==";
            volume = "2206441.67";
        }
    );
}

First if you want to send your response back to the point from where initiated the API call you need to write completion handler and send your response model with the handler; you can take reference from Closures for Swift.

Also apart from that I noticed few errors in your decodable structure, for example you are expecting 'difference' as float type but the example JSON you have posted contains 'difference' as String and it applies for all your float and double values.

Also it will be a good practice If we will declare all the variable optional in decodable structure as if anytime any parameter won't come in response there won't be any problem in parsing it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM