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

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

Leave a comment