Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Struct to JSON

Tags:

json

c#

struct

I have a struct, I want to convert it to JSON and save it as local file.

I couldn't find any source that explain how to convert a C# struct into a JSON.

I am using a console application for that, not a webservice/web, etc.

like image 730
m0fo Avatar asked Aug 22 '12 20:08

m0fo


Video Answer


1 Answers

JavaScriptSerializer Class

var serializer = new JavaScriptSerializer();
YourStruct myStruct = new YourStruct(x,y,z);
var json = serializer.Serialize(myStruct);

JSON.NET

The other alternative JSON.net, it do not depends on System.Web.* assemblies:

YourStruct myStruct = new YourStruct(x,y,z);
var json = JsonConvert.SerializeObject(myStruct);
like image 139
Marcelo De Zen Avatar answered Oct 04 '22 11:10

Marcelo De Zen