0

I am working with a REST API from C# and utilizing RESTSharp, though this isn't really a question about REST, just some background detail.

To get the dataset I am working with I get a Returned Dictionary<string,object>. Once I make the changes to the data I need to submit, I have to submit the data back as a Dictionary<string, object>[]. What is the simplest way of converting the Dictionary<string, object> directly to a 1 lengthed array?

I know how to do it when creating the Dictionary[] from scratch, but having a brain fart on how to do it with the regular Dictionary already created, and it has over 300 records in it, so don't really want to parse through it.

If I do something like this:

Dictionary<string, object>[] tempArray = new Dictionary<string, object>[]
        {
            new Dictionary<string, object>{ {"Data", Data } }
        };

This ends up with a Data record with the dictionary and that isn't what I want.

Another way to say this, is when I serialize the record to JSON I get back I get this:

{
  "rec1": "string",
  "rec2": 0
}

When I need to submit it back it needs to be this:

[
  {
    "rec2": 0,
    "rec1": "string"
  }]
Earthworm
  • 116
  • 7
  • Create a dictionary list of `List>`. And add the dictionary into list `dictionaryList.Add(/* Your dictionary */)`. Is this what you want? And please specify what is your `Data`. – Yong Shun Jul 14 '23 at 15:43
  • See, brain fart. Was so stuck on it having to stay within a Dictionary I didn't think outside of the box. Yes, this Serializes back to the format I need. Thank you. – Earthworm Jul 14 '23 at 15:52

1 Answers1

0

For converting the Dictionary<string, object> to Dictionary<string, object> list/array with Data:

  1. Create a List<Dictionary<string, object>> instance.
  2. Add the Data into List<Dictionary<string, object>> instance.
using System.Collections.Generic;

List<Dictionary<string, object>> tempList = new List<Dictionary<string, object>>();
tempList.Add(Data);

Or

Create a List<Dictionary<string, object>> instance and initialize it with the value.

List<Dictionary<string, object>> tempList = new List<Dictionary<string, object>>()
{
    Data
};
Yong Shun
  • 35,286
  • 4
  • 24
  • 46
  • Thank you for answering putting this where I can accept it as the answer. Was wondering if I should just delete the question, but this works better. Thank you again! – Earthworm Jul 14 '23 at 17:25