-3

I want to parse this code into an object in C#. I was trying to find an answer but every post included data with key values which this doesn't have. Every piece of data is separated by semicolons.

[
  [
    1499040000000,
    "0.01634790",
    "0.80000000",
    "0.01575800",
    "0.01577100",
    "148976.11427815",
    1499644799999,
    "2434.19055334",
    308,
    "1756.87402397",
    "28.46694368",
    "17928899.62484339"
  ]
]
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 1) According to [Curious concept](https://jsonformatter.curiousconcept.com/) it is valid Json. What is the result when you call [Newtonsoft's](https://www.newtonsoft.com/json) `JsonConvert.DeserializeObject(jsonstring)`? – LosManos Jan 03 '19 at 22:05
  • I don't know what you mean with Product. – user3434894 Jan 03 '19 at 23:04
  • You might be able to deserialize this to a `List` for some appropriate `T`, where the members of `T` correspond to the array entries and you are using `ObjectToArrayConverter` from [here](https://stackoverflow.com/a/39462464/3744182). Or you could just load using `JArray.Parse(jsonString)` from [tag:json.net]. – dbc Jan 04 '19 at 04:58
  • 1
    [See here](https://app.quicktype.io?share=9Pw1xdo0RVruD9g4XjW6) for an auto-generated class to parse your JSON. – Uwe Keim Jan 04 '19 at 08:28
  • You mention semicolons (`;`), did you mean commas (`,`)? Just so that we know that you don't have a different file construct than the one you posted. In any case, what you have is a collection (`[...]`) or collections (`[...]`) of objects (`149904000000` and `"0.01634790"`), since the objects doesn't have any common type, strings, integers, floating point numbers, you're probably best off by deserializing this as `List>` or `object[][]`, so `JsonConvert.DeserializeObject(json)` should work. – Lasse V. Karlsen Jan 04 '19 at 08:31
  • 2
    Possible duplicate of [C#: Parsing a non-JSON array-only api response to a class object with x properties](https://stackoverflow.com/q/48429702). – dbc Jan 04 '19 at 17:39

1 Answers1

2

To quick and easy get the object that Visual Studio think of the Json, you can copy the complete Json and then go to Edit > Paste Special > Paste JSON As Classes. For me, that generates the following for the complete Json that you posted:

    public class JsonClass
    {
        public object[][] Property1 { get; set; }
    }

(This could potentionally be object[] depending on incoming Json.)

This should be possible to JsonConvert into a List<JsonClass> and after that, depending on your scenario, try to parse the data to correct data type if that is needed.

I think that another way of doing it is also as @dbc mentioned in the comment:

You might be able to deserialize this to a List<T> for some appropriate T, where the members of T correspond to the array entries and you are using ObjectToArrayConverter<T> from here. Or you could just load using JArray.Parse(jsonString) from json.net.

jwweiler
  • 244
  • 4
  • 18
  • I am using the class you provided with this code `JsonConvert.DeserializeObject >(json);` Getting the error _Unhandled Exception: Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'tradingbot.Domain.JsonClass' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly._ – user3434894 Jan 04 '19 at 13:48
  • @user3434894 - The outer `JsonClass` object isn't necessary, just do `JsonConvert.DeserializeObject(jsonString);` – dbc Jan 04 '19 at 17:23