nichu09
nichu09

Reputation: 892

construct json dynamically in c#

i created one dynamic json string like this

  {"attributecollection":{"Soid":"so1","BUCODE":"bu1","ClientCode":"clc1","PaymentStatus":"true"},"input":{"Soid":"so1","BUCODE":"bu1","ClientCode":"clc1","PaymentStatus":"true"},"output":{}}

so after that i can access values from this string by converting to dynamic and get the value.but in my scenario i want to add values in output after some time.so how i will do that.after converting to dynamic can i add values or again i want to construct the json string with including output.is there any other option for this.help appreciated.

Upvotes: 2

Views: 1420

Answers (1)

Elvedin Hamzagic
Elvedin Hamzagic

Reputation: 855

You probably want to convert JSON string to some native type to access it's values, change them, and create new JSON string from that data again. There are plenty of solutions on the internet that you can find, including many thirt-party libraries. I can recommend you http://sourceforge.net/projects/jsonlib/?source=directory written by myself, and here is an example how to use it:

String jsonString = "{\"key1\":\"value1\",\"key2\":2}"; // input string
JSONObject jsonObject = JSONObject.Parse(jsonString);   // parse string
jsonObject["key1"] = 1;                                 // modify key1
jsonObject["key3"] = "value3";                          // add key3
jsonString = jsonObject.ToString();                     // create JSON string

I hope that's what you are looking for. If you are interesting for more information of how to use this library refer to the included examples and wiki pages.

Upvotes: 1

Related Questions