How to serialize an object to json through setting json field name from object’s property value?
I am using NewtonsoftJson as a json library.
public class Data
{
public string Question {get;set;} = "Test Question?";
public string Answer {get;set;} = "5";
}
expected json output:
{
"Test Question?": {
"Answer": "5"
}
}
2
Answers
You can use a dictionary for that:
or if you are using Newtonsoft, you can use the
JsonConvert.SerializeObject
method for serialization, with the same input.Just for a record