0

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))
                }
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
Sathyan
  • 138
  • 1
  • 10
  • Get a stick, find the developer how thought this format was a "good idea" and ... well, look all passive aggressively at them . You will need to manually decode those properties by overriding the `int(from: Decoder)` – MadProgrammer Apr 03 '22 at 07:04
  • First of all forget `SwiftyJSON`. It's a honorable library but became obsolete in favor of `Codable`. An answer depends on whether you want the `String` type or whether the string can be converted to `Double`. And please add the code you are using. – vadian Apr 03 '22 at 07:05
  • @vadian I want the Double value..also added the code I am using to parse it. – Sathyan Apr 03 '22 at 07:06
  • You are not printing the JSON. You are printing a NSDictionary. Please edit your question and post your actual json. `print(String(data: response.data!, encoding: .utf8) ?? "nil")` – Leo Dabus Apr 03 '22 at 07:21
  • [Support multiple types in codable — swift](https://medium.com/@sajjadsarkoobi/support-multiple-types-in-codable-swift-de461689e21b) might help – MadProgrammer Apr 03 '22 at 07:23

0 Answers0