0

I want to parse json in swift 4 using codable. I know how to parse json using codable of known keys. But I am unable to parse json with unknown keys. Please help me to solve this issue

I am trying the following object to parse

additionalInfo =             (
                            {
                expensesInfo =                     (
                                            {
                        "Hotel Expenses" = "426.0";
                    },
                                            {
                        "Cab Expenses" = "46.0";
                    },
                                            {
                        "Trekking Expenses" = "160.50";
                    },
                                            {
                        "Train Fare" = "70.56";
                    },
                                            {
                        "Food Cost" = "270.60";
                    },
                                            {
                        "Bar Cost" = "448.90";
                    }
                );
            }
        )

I know only key is "additionalInfo" whatever data is inside this key I don't know. Then how to parse it in swift 4.0

Thanks.

  • 1
    I don't believe theres a way to decode JSON and not know anything about its payload. The API should tell you what to expect. However if there are some dynamic keys, I'd try looking at [this answer](https://stackoverflow.com/a/50257595/8397048) – SirCJ Aug 24 '18 at 12:35
  • suppose I know expensesInfo key then how to decode data that is inside this key. I am very afraid. – Bajrang Sinha Aug 24 '18 at 12:48
  • Are you responsible for the JSON? If no please blame the owner of the web service for that weird – an array of dictionaries with **one** key/value pair respectively – JSON structure. If you don't know the keys don't use `Codable`. – vadian Aug 26 '18 at 09:09
  • Yes whatever data is present inside additional info is I send to backend and backend same response me. but this is dynamic by user. user can write any thing. And as I'm not getting any idea how to parse. I'm not using codable. Thanks. – Bajrang Sinha Aug 28 '18 at 05:47

0 Answers0