This content originally appeared on DEV Community and was authored by Debashish Pal
Use Case:
We have this Object and you want to convert it into JSON.
Sounds simple. Right !!
However, the trick here is that you need to add couple of fields dynamically to the object, before converting.
Take a pause
Think about the solution
?
Think again
Scroll down for the solution
...
...
...
...
...
...
Solution
Run the below code in Linqpad (https://www.linqpad.net/), to see it working.
Make sure you are pressing F4, and adding the Nuget package i.e. Newtonsoft.Json
I have created this extension method AddFieldsAndConvertToJson. We are simply passing array of tuple's with the required field name & data to the extension.
Check the output below. The fields are getting added to the employee object dynamically, and the JSON is being outputted.
There might be a specific situation, where this extension might come in handy.
Hope you find it useful.
Happy Coding !!
This content originally appeared on DEV Community and was authored by Debashish Pal
Debashish Pal | Sciencx (2021-04-23T16:13:18+00:00) Solve the scenario – using JObject in Dotnet. Retrieved from https://www.scien.cx/2021/04/23/solve-the-scenario-using-jobject-in-dotnet/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.