0

I have an API endpoint which is receiving a data string which looks like the following:

"[[0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0],[[],[],[],[[17.637329000012972,17.637329000012972]],[],[],[],[],[]]]"

I would now like to convert this json object into an array which I can query, but I'm not sure where to start.

I have tried converting to a jagged array using

var result = JsonConvert.DeserializeObject<SomeType[][]>(json); where SomeType has been int, string, double etc etc

The problem seems to lie with each array being anonymous so it's proving difficult to model. To further complicate things some of the arrays found in the base array are also jagged arrays. As it happens I'm only really interested in the first array so if anyone can offer an easy way of extracting this I would be very grateful. Thanks in advance

Ryan
  • 233
  • 2
  • 9
  • 1
    How about `List> results = JsonConvert.DeserializeObject>>(json);`. Also I'm not sure what exactly you would query of of this, what would your query be based on? – Trevor Mar 05 '21 at 19:52
  • Unfortunately deserializing to List> throws an error. This data is passed to my API from an xApi elearning module. The first array within the array refers to the sections of the course, if it's 1 then it's complete if it's 0 it is not. This array can then be used to calculate the users progress in the course – Ryan Mar 05 '21 at 20:00
  • I just took your json above and could deserialize to the `List>` which gives me 2 objects in that list, with the first list having 16 objects and the last gives me 9 with no issues and matches your data; there's no errors. Could you be specific about `throws an error`? – Trevor Mar 05 '21 at 20:05
  • 1
    Another thing to mention, that data above is already json, all you need to do is deserialize it, not serialize it again. `I need to deserialize this string so I can extract it's values. As the data is of type string I have converted it to JSON using`, you don't need to convert anything. – Trevor Mar 05 '21 at 20:10
  • 2
    Apologies yes, initially an error was thrown relating to the data string not being an object - serializing as JSON seemed to fix this but was obviously not due to this and I didn't remove. Removing the serialize does indeed return 2 objects, the first being exactly what I'm after so thank you – Ryan Mar 05 '21 at 20:14
  • 1
    Does your JSON really include the outer quotes `"` and `"` shown in your question? If so it's a **JSON string** not a **JSON array**. In fact it might be [double-serialized JSON](https://stackoverflow.com/q/25559179/3744182), which typically arises due to a bug on the sending side. If so, you have to deserialize it first as a string, then secondly as a `List>`. – dbc Mar 05 '21 at 21:15

0 Answers0