Question

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?

Was it helpful?

Solution

You can get the output you want by creating a custom JsonConverter for your JSONSubTestClass2 class like this:

class JSONSubTestClass2Converter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return (objectType == typeof(JSONSubTestClass2));
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        JSONSubTestClass2 jstc2 = (JSONSubTestClass2)value;
        JObject jo = new JObject();
        jo.Add(jstc2.id, JObject.FromObject(jstc2.info));
        jo.WriteTo(writer);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

Then, serialize your classes like this:

JsonSerializerSettings settings = new JsonSerializerSettings();
settings.Converters.Add(new JSONSubTestClass2Converter());
settings.Formatting = Formatting.Indented;

// Convert to JSON
string json = JsonConvert.SerializeObject(jtc, settings);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top