0

I need to serialize a JSON string containing the following:

{
    commandId;
    id;
    params:
    {
        userId;
        password;
    }``
}

The code I was given uses Qt and they declare a QJsonObject paramsObj and a QJsonObject cmdObj; they fill the field values and finally perform a cmdObj.insert("params", QJsonValue(paramsObj));

params is a keyword for VS and C# so I can't declare a class with that name, but this is the way the device will understand my JSON strings. I am fairly new to JSON and looked at the .Net class and the Newtonsoft library but can't find how to perform the insert of a JSON object inside another, assigning an arbitrary name to it. Can anyone shed some light? Thank you.

Tleilax
  • 77
  • 10

1 Answers1

1

You just need to escape params with @ like this:

public class MyObject
{
    public Params @params { get; set; }
}

This produces:

{
  "params": {}
}

Or use Params as property name and use CamelCasePropertyNamesContractResolver.

weichch
  • 9,306
  • 1
  • 13
  • 25
  • I ended up following what stated in this response: https://stackoverflow.com/questions/16704733/how-can-i-use-a-reserved-keyword-as-an-identifier-in-my-json-model-class It works perfectly without adding the @ – Tleilax Apr 09 '20 at 13:10