json - How to generate a Dictionary<> object in c# -
I want to create a dictionary & lt;> object and that dictionary object should include name and value attribute. For example:
dictionary & lt; String, Object & gt; Dict = new dictionary & lt; String, Object & gt; ();
And when I serialize that dictionary into json string then it should be accessed like this:
dict [0] ["name "] ==" product name "; // Property Name Dictionary [0] ["Price"] == "Product 1"; // pricing instructions [1] ["name"] == "description"; // Property Name Dictionary [2] ["Price"] == "Product 1 Category"; // value ............................
But I can not find a way for this is this. Can someone suggest me how can we do this?
Edit: -
Actually I get a JSON string from Ajax Post: -
= "{" Name ":" firstname "," value ":" john "}",
and once I am able to get it deserialize that string in the format below: - < / P>
var dictDynamic = sear.Deserialize & lt; Dynamic & gt; (Str);
And as a result, I am receiving properties like this:
dictdynamic [0] ["name"]
and such property value: -
dictDynamic [0] ["value"]
But the problem is that I do this Want to generate a model string in the Jason String format on top of the server side and after that it has to be digitized in samples.
A dictionary does not allow you to provide two keys with the same value. Therefore, 0
does not work as a key.
Perhaps the best thing is to make an object to keep your information.
public class product {public string id {get; Set;} public string name {get; Set;} public string value {get; Set;}}
Then create some objects.
Product Product = New Product (); Product.id = "0"; Product.Name = "My Super Widget"; Product.Value = "500"; // then add that product to dictionary & lt; String, product & gt; Products = New Dictionary & lt; String, product & gt; (); Products.Add (product.ID, Products); // so you can access it in the products ["0"] this way. // Its value is "My Super Widget"
Want to serialize JSON? Let's use it.
string json = JsonConvert.SerializeObject (products);
Comments
Post a Comment