1 year ago

#368779

test-img

Sathyan

How to parse json that has multiple type(datatype) of values for single key

I am using Alamofire with SwiftyJson for api calls.On one of the api Call, I need to parse a response that has array of objects. For Example sample json:

 totalValues =(
            {                   
                title = "Sub-Total";
                value = 564;
            },
            {                 
                title = "Convenience";
                value = 40;
            },
            {                  
                title = "Transaction Total";
                value = 604;
            },
            {                   
                title = "Old Wallet";
                value = "-41.6667";
            },
            {    
                title = "TOTAL";
                value = 562;
            },
            {    
                title = "New Wallet Balance";
                value = 0;
            }
        );

Now I struck at how to create model class for this?Which data type I need to give for the value.If I add Any to the key..I am getting Type 'Total'(class name) does not conform to protocol 'Decodable'

Or is there any other way to parse this??

Can someone give me solution for this??

Thanks in advance

I am parsing the response json like below:

if response.result.value != nil {
                    do{
                        let jsonObject = try JSONDecoder().decode(Response.self,from: response.data!)
                        single(.success(jsonObject))
                    }catch let error {

                         single(.failure(error))

                    }
                }else if let error = response.result.error as NSError? {
                    single(.failure(error))
                }else if let error = response.result.error {
                    single(.failure(error))
                }

swift

parsing

alamofire

0 Answers

Your Answer

Accepted video resources