Reputation: 1582
I am just getting started with C# and I'm a little stuck.
How do I create a dictionary that contains a mix of string,string, string,int, and string,object?
this is what it would look like:
var values = new Dictionary<????>
{
"key0":
{
"key1": "stringValue1",
"key2": "stringValue2",
"key3": "stringValue3",
"key4": 10000
},
"key5": "stringValue4",
"key6": "stringValue5"
};
I am using this as the body for a POST request. Sample code from here
var body = new FormUrlEncodedContent(values);
var url = "http://url.com/endpoint"
var resp = await client.PostAsync(url, body);
var respString = await response.Content.ReadAsStringAsync();
Upvotes: 2
Views: 9648
Reputation: 27039
This is what you said in your comment:
please note that I come from a JavaScript background where you can do whatever you want with a dictionary.
Yes you can do that in JavaScript but you need to learn and understand that C# is a strongly typed language. That does not mean you have to be strong to type it, but it means the types are known at compile time (mostly). Please read into that.
Anyhow, to do what you want to do, you can do it with a Dictionary
but it will not be pretty and your fellow C# developers will not be happy with you. So how about you give your keys and values some context by creating a class (OOP). Something like below:
public class Rootobject // Give it a better name
{
public Key0 key0 { get; set; }
public string key5 { get; set; }
public string key6 { get; set; }
}
public class Key0
{
public string key1 { get; set; }
public string key2 { get; set; }
public string key3 { get; set; }
public int key4 { get; set; }
}
Now you have your class so you can create one or more instances of it:
var ro = new Rootobject();
ro.key5 = "stringValue4";
ro.key6 = "stringValue5";
var key0 = new Key0();
key0.key1 = "stringValue1";
key0.key2 = "stringValue2";
key0.key3 = "stringValue3";
key0.key4 = 1000; // See we cannot put a string here. Different than JavaScript
ro.key0 = key0;
Now you want to POST this and send it over the wire so you need to serialize it. But in your case you need to serialize it to JSON. Therefore, get NewtonSoft so it can do all the heavy lifting for you--again not saying you are not strong. Then all you need to do is this:
var json = JsonConvert.SerializeObject(ro);
Now json
will be exactly like this and you can POST it to wherever:
{
"key0": {
"key1": "stringValue1",
"key2": "stringValue2",
"key3": "stringValue3",
"key4": 1000
},
"key5": "stringValue4",
"key6": "stringValue5"
}
How did I create the class?
The class above named RootObject
, you can either create it manually or ask Visual Studio to do it for you. I am lazy so I asked Visual Studio to do it. If you are lazy then see my answer here.
Upvotes: 3
Reputation: 7019
Check this link:
HttpClient PostAsJsonAsync request
You can cast it as
var myData = await response.Content.PostAsJsonAsync<Dictionary<string, dynamic>>(...);
Then you can use it as:
string myStr = myData["key5"];
Although I would recommend you make a class with the structure and use the class name within <>
Sample class:
class MyData {
public MyData2 key0 { get; set; }
public string key5 { get; set; }
public string key6 { get; set; }
}
class MyData2 {
public string key1 { get; set; }
public string key2 { get; set; }
public string key3 { get; set; }
public int key4 { get; set; }
}
Now you can use this as:
var myData = await response.Content.PostAsJsonAsync<MyData>(...);
Upvotes: 2
Reputation: 6965
You can use dynamic. However, in the end, form post will convert everything into string, so you might wanna convert int and object into string first. Then, you can just use Dictionary<string, string>
Dictionary<string, dynamic> Dict = new Dictionary<string, dynamic>();
Dict.Add("string1", "1");
Dict.Add("string2", 2);
Dict.Add("string3", new object());
Upvotes: 2