0

I have an API that returns a JSON of this structure:

{
   "1":[
      {
         "name":"One"
      },
      {
         "name":"Two"
      }
   ],
   "2":[
      {
         "name":"Three"
      }
   ]
}

I need to deserialize it in C# using Newtonsoft.Json. I have tried deserializing in a List<List<Response>> but it didn't work.

I understand that the JSON is not necessarily good, but I can't change it since it's on a third party server.

Every help is appreciated.

  • Are the indexes contiguous? Do they always start at `"1"`? As a first step, you could deserialize to a `Dictionary>` and then sort the key-value pairs by key and finally select only the value – derpirscher Jul 22 '22 at 14:10
  • That's a dictionary, not an array. Those are keys, not indexes. You'll have to use a `Dictionary` to deserialize this document before you can change that dictionary into a list – Panagiotis Kanavos Jul 22 '22 at 14:12
  • Yep, dictionary did it. Thanks guys – Mihail Panagiotopoulos Jul 22 '22 at 14:21
  • Does this answer your question? [json deserialization to C# with dynamic keys](https://stackoverflow.com/questions/65727513/json-deserialization-to-c-sharp-with-dynamic-keys) – Charlieface Jul 22 '22 at 17:33

1 Answers1

0

Deserialize into a dictionary (or SortedDictionary, if the order of the elements in the dictionary matter), then take the dictionary's Values collection and run...

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 23 '22 at 07:12