0

I receive the following JSON response from a web server:

[
  [
    1499040000000,
    "0.01634790",
    "0.80000000",
    "0.01575800",
    "0.01577100",
    "148976.11427815",
    1499644799999,
    "2434.19055334",
    308,
    "1756.87402397",
    "28.46694368",
    "17928899.62484339"
  ]
]

How can I parse it to an array using System.Text.Json?
I've already tried String.Split() and String.Replace() (as the brackets and quotes appear in elements) but it's an "ugly" approach.

Any suggestions will be very much appreciated.

0

1 Answer 1

1

That JSON represents an Array of arrays (well, a single nested array here).
You can parse it as a List(Of Double()) or List(Of List(Of Double)).

The mixed data Type (strings and numbers), can be converted to all numbers, setting a JsonSerializerOptions that specifies the NumberHandling to use, here set to AllowReadingFromString.

Dim options = New JsonSerializerOptions() With {
    .NumberHandling = JsonNumberHandling.AllowReadingFromString
}

Dim listOfDouble = JsonSerializer.Deserialize(Of List(Of Double()))(json, options)

You could also deserialize to a List(Of Object()) and perform the conversion later or just read the values as strings, calling .ToString() when you read an element of the List.

 Dim listOfObjects = JsonSerializer.Deserialize(Of List(Of Object()))(json)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.