Whatever they tell you C# is somehow a fancy language. One of the reasons for this is that you may use the hard work of plenty of other good developers for free and without even thinking of how to make your own libraries for trivial stuff.
E.g., if you have some input data, coming as a JSON, which should be taken from your application, edited and returned as a JSON, C# can process the whole action with the assistance of the using System.Web.Script.Serialization; magic library.
So, lets say we work in a shop for tables and we obtain the following JSON:
string strXML = @”{“”ObjectType”” : “”Table””, “”Price”” : 512, “”Shops”” : [“”IKEA””,””VityataShop””]}”;
Not exactly JSON, but you get the idea 🙂 C# and the Web.Script library helps us to make an Object out of it and to edit it there. Then we may change data in the object (of course!) and export the JSON back. Nice and shiny.
Like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
using System; using System.Web.Script.Serialization; class Program { static void Main() { string strXML = @"{""ObjectType"" : ""Table"", ""Price"" : 512, ""Shops"" : [""IKEA"",""VityataShop""]}"; Sku objSku = Sku.BuildSkuFromJson(strXML); Console.WriteLine(objSku.ObjectType); Console.WriteLine(objSku.Price); foreach (var item in objSku.Shops) { Console.WriteLine("Present in {0}", item); } Console.WriteLine("Now lets deserialize back to JSON:"); objSku.Price = 1123; objSku.Shops = new string[] { "IKEA2", "VityataShop4", "Vitoshacademy5" }; var serializer2 = new JavaScriptSerializer(); Console.WriteLine(serializer2.Serialize(objSku)); } } class Sku { public string[] Shops { get; set; } public string ObjectType { get; set; } public int Price { get; set; } public static Sku BuildSkuFromJson(string json) { var serializer = new JavaScriptSerializer(); return serializer.Deserialize<Sku>(json); } } |
Here is the console:
Enjoy it! 🙂