Hoping I don't have to reinvent the wheel here but does anyone know if there is a class in C# similar to the one supplied by Adobe for AS3 to convert a generic object to a JSON string?
For example, when I encode an array of objects.
new JSONEncoder(arr).getString();
Output:
[
{"type":"mobile","number":"02-8988-5566"},
{"type":"mobile","number":"02-8988-5566"}
]
in C#:
var jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string json = jsonSerializer.Serialize(yourCustomObject);
I recommand using Json.NET. It's not part of .Net's the core libraries, but it is very widely used, including by a lot of Microsoft's products. Also it's the single most used nuget package. And it's both easier to use than JavaScriptSerializer
and more efficient.
var jsonString = JsonConvert.SerializeObject(someObjet);
var myObject = JsonConvert.DeserializeObject<MyType>(jsonString);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With