Return json string with proper name

In C# 7, it is now possible to have tuple return type. You would have to use a var to store the return value. However if you simply return the var as a json, the json will be slightly messed up as it will simply be shown as item1, item2, etc.

In order to make sense of what is being return, you would need to create a new var to “reformat” the naming of the item in the return. Thereafter you can serialize the object. Please refer to the follow for a sample


var obj = new {Id = thing.Id, Name = thing.Name, Age = thing.Age};

Reference: https://stackoverflow.com/questions/331976/how-do-i-serialize-a-c-sharp-anonymous-type-to-a-json-string

Advertisements
This entry was posted in C#, Json and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s