3

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.

  • 5
    It looks like you are trying to generate JSON using strong concatenation. Don't do that. Create the classes, and serialise to JSON instead. – mjwills Jun 10 '21 at 22:58

2 Answers2

3

You can have double quotes("") to support multi-line:

var input = @"{
        ""method"": ""api.notifications.add"",
            ""params"": {
                ""name"": ""sequence.state.changed"",
                ""options"": {
                    ""include_print_record"": true,
                    ""include_layout"": true
                }
            },
            ""id"": 0,
            ""jsonrpc"": ""2.0""
        }";

dotnet fiddle link: https://dotnetfiddle.net/5sBzS1

Umang
  • 815
  • 5
  • 17
1

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);
    }
}

Result

Then if you need to convert your object back to a Json string

Result1

string jsonResponse = JsonSerializer.Serialize(inputObject);
traveler3468
  • 1,557
  • 2
  • 15
  • 28
  • Another key thing that OP wants is formatting. If you add formatting options to your serialize then that would be amazing. – Umang Jun 11 '21 at 06:00
  • While not necessarily what I was asking for what I actually needed good job AndrewE. – Michael Hunsaker Jun 11 '21 at 15:24
  • Trying to send data over a tcp connection then receive continuous responses. Never done anything at this level before riding the struggle bus pretty hard atm. – Michael Hunsaker Jun 11 '21 at 18:11