-2

I want to parse json in below in swift 4 but sellOrders and buyOrders are return empty.

JSON:

{
    marketName: "btc_tl",
    chartData: [ ],
    sellOrders: {
        39970.00: "1.42476125",
        39980.00: "0.92996500",
        39990.00: "0.03751539",
        39998.00: "0.33341067",
        40000.00: "0.16707899",
        40320.00: "0.01657837",
        40327.00: "0.24812160",
        40331.00: "0.20076547"
    },
    buyOrders: {
        39970.00: "1.42476125",
        39980.00: "0.92996500",
        39990.00: "0.03751539",
        39998.00: "0.33341067",
        40000.00: "0.16707899",
        40320.00: "0.01657837",
        40327.00: "0.24812160",
        40331.00: "0.20076547"
    },
    marketMatches: [],
    userMatches: []
}

I use code in below. How can I reach sellOrders or buyOrders in that json?

 let jsonResponse = try JSONSerialization.jsonObject(with:
            dataResponse, options: []) 
trincot
  • 317,000
  • 35
  • 244
  • 286
fcoskun
  • 23
  • 10

1 Answers1

1

Your JSON is invalid. Valid JSON has all keys quoted with double quotes, which is not the case in your input. See Do the JSON keys have to be surrounded by quotes?.

You can validate JSON on free online services, such as JSON formatter & validator. For your input it will say:

Error: Strings should be wrapped in double quotes. [Code 17, Structure 2]

If you are the producer of that "JSON", then correct it. Otherwise contact the provider for them to correct this.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • I'm not the producer but this works with JS so the producer doesn't want to change it. – fcoskun Dec 28 '18 at 06:49
  • 1
    If you really want to stick to this data source then you'll have to write your own parser for this. If the structure is always the same it might not be that difficult. It is a known misunderstanding with JS coders that if it works in JS it can be called JSON. Obviously not true. – trincot Dec 28 '18 at 08:18