Using Json.NET in C#, I am having troubles when serializing a class, in which I have a need for a custom property name.
This is what I have now:
{
"baseName": {
"subName1": {
"name": null,
"longName": null,
"asd1": null,
"asd2": null
},
"subName2": [
{
"id": "ID_NUMBER",
"info": {
"someInfo1": "asd",
"someInfo2": "asd2"
}
}
]
}
}
This is what I want:
{
"baseName": {
"subName1": {
"name": null,
"longName": null,
"asd1": null,
"asd2": null
},
"subName2": [
{
"ID_NUMBER": {
"someInfo1": "asd",
"someInfo2": "asd2"
}
}
]
}
}
That is, instead of having a key with id and value ID_NUMBER, I want the ID_NUMBER to be the key of the object containing the keys someInfo1 and someInfo2.
The top JSON code is generated using these classes (sorry for the bad names):
class JSONTestClass
{
public JSONBaseTestClass baseName;
}
class JSONBaseTestClass
{
public JSONSubTestClass1 subName1;
public List<JSONSubTestClass2> subName2;
}
class JSONSubTestClass1
{
public string name;
public string longName;
public string asd1;
public string asd2;
}
class JSONSubTestClass2
{
public string id;
public JSONInfoTestClass info;
}
class JSONInfoTestClass
{
public string someInfo1;
public string someInfo2;
}
And this:
private void MakeJSON()
{
// This value can be changed at runtime
string specificId = "ID_NUMBER";
JSONInfoTestClass jitc = new JSONInfoTestClass();
jitc.someInfo1 = "asd";
jitc.someInfo2 = "asd2";
JSONTestClass jtc = new JSONTestClass();
JSONBaseTestClass jbtc = new JSONBaseTestClass();
JSONSubTestClass1 jstc1 = new JSONSubTestClass1();
JSONSubTestClass2 jstc2 = new JSONSubTestClass2();
jstc2.id = specificId;
jstc2.info = jitc;
List<JSONSubTestClass2> list = new List<JSONSubTestClass2>();
list.Add(jstc2);
jbtc.subName1 = jstc1;
jbtc.subName2 = list;
jtc.baseName = jbtc;
// Convert to JSON
string json = JsonConvert.SerializeObject(jtc, Formatting.Indented);
tbxJSONOutput.Text = json;
}
Which changes are needed so I can get a JSON output corresponding to the second JSON response mentioned above?