How to achieve your desired JSON format using C # .NET HashTable

How to achieve below mentioned JSON format using C # .NET HashTable

{"DoWorkResult": [ {"Perimeter":"55}, {"Mortgage":"540"}, {"Area":"1000"} ] } 

I tried to do this using a hashtable with an example as shown below

  Hashtable hashtable = new Hashtable(); hashtable.Add("Area", 1000); hashtable.Add("Perimeter", 55); hashtable.Add("Mortgage", 540); 

But the result is as shown below

 {"DoWorkResult": [ {"Key":"Perimeter","Value":55}, {"Key":"Mortgage","Value":540}, {"Key":"Area","Value":1000} ] } 

Note: I am returning the actual Hash table in the WCF service method, and I am using the ajax call to read the output from the backend.

Ajax method that I use in the interface:

 $.ajax({ type: 'POST', url: '/Service.svc/DoWork', success: function(data) { alert(data); } }); 
+4
source share
1 answer

Using JavaScriptSerializer and Json.Net

 var list = new ArrayList(); list.Add(new { Area = 1000 }); list.Add(new { Perimeter = 55 }); list.Add(new { Mortgage = 540 }); var s1 = new JavaScriptSerializer().Serialize(new { DoWorkResult = list }); var s2 = JsonConvert.SerializeObject(new { DoWorkResult = list }); 
+7
source

All Articles