I need to make this text:
{
"method": "api.notifications.add",
"params": {
"name": "sequence.state.changed",
"options": {
"include_print_record": true,
"include_layout": true
}
},
"id": 0,
"jsonrpc": "2.0"
}
Into a string with c# such as like this:
input = @"{
"method": "api.notifications.add",
"params": {
"name": "sequence.state.changed",
"options": {
"include_print_record": true,
"include_layout": true
}
},
"id": 0,
"jsonrpc": "2.0"
}";
It needs to retain the formatting that it has. I have tried a number of things including putting a back slash before each quote and obviously putting an @ symbol before the first quote.
Depending on your type of flavor, I like to use backslashes.
string input =
"{\"method\": \"api.notifications.add\"," +
"\"params\": " +
"{\"name\": \"sequence.state.changed\"," +
"\"options\": " +
"{\"include_print_record\": true,\"" +
"include_layout\": true}" +
"}," +
"\"id\": 0," +
"\"jsonrpc\": \"2.0\"" +
"}";
However as mentioned above in the comments, you would be so much better off creating a Class or Struct and then serializing the json data.
It might seem like a lot of work but you will be thanking yourself in the long run.
Here is a quick example to help you get started.
namespace Foo
{
public class MyInputObject
{
[JsonPropertyName("method")]
public string Method { get; set; }
[JsonPropertyName("params")]
public Params Params { get; set; }
[JsonPropertyName("id")]
public long Id { get; set; }
[JsonPropertyName("jsonrpc")]
public string Jsonrpc { get; set; }
}
public class Params
{
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("options")]
public Options Options { get; set; }
}
public class Options
{
[JsonPropertyName("include_print_record")]
public bool IncludePrintRecord { get; set; }
[JsonPropertyName("include_layout")]
public bool IncludeLayout { get; set; }
}
// Entry Point For Example.
public void Bar()
{
string input =
"{\"method\": \"api.notifications.add\"," +
"\"params\": " +
"{\"name\": \"sequence.state.changed\"," +
"\"options\": " +
"{\"include_print_record\": true,\"" +
"include_layout\": true}" +
"}," +
"\"id\": 0," +
"\"jsonrpc\": \"2.0\"" +
"}";
MyInputObject inputObject = JsonSerializer.Deserialize<MyInputObject>(input);
}
}
Then if you need to convert your object back to a Json string
string jsonResponse = JsonSerializer.Serialize(inputObject);