C# Multiline 多双引号字符串

C# Multiline multi-double quote string

我需要制作这段文字:

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

用 c# 转换成字符串,例如:


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

它需要保留它的格式。我尝试了很多方法,包括在每个引号前放一个反斜杠,显然在第一个引号前放一个 @ 符号。

你可以用双引号("")来支持多行:

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

网络 fiddle link: https://dotnetfiddle.net/5sBzS1

根据你的口味类型,我喜欢使用反斜杠。

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

然而,正如上面评论中提到的,创建一个 Class or Struct 然后序列化 json 数据会好得多。

它可能看起来工作量很大,但在漫长的过程中你会感谢自己 运行。
这里有一个简单的例子可以帮助你入门。

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

然后如果您需要将对象转换回 Json 字符串

string jsonResponse = JsonSerializer.Serialize(inputObject);