C# 多行多双引号字符串

3

我需要将这段文字翻译:

{
    "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"
    }";

它需要保留其原有的格式。我已经尝试了许多方法,包括在每个引号前加上反斜杠,显然在第一个引号前加上 @ 符号。


5
看起来你正在尝试使用字符串拼接生成JSON。不要这样做。创建类并将其序列化为JSON。 - mjwills
2个回答

3

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

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链接:https://dotnetfiddle.net/5sBzS1


1

根据您所选择的口味,我喜欢使用反斜杠。

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

然而,正如在评论中提到的那样,你最好创建一个结构体,然后对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);
    }
}

Result

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

Result1

string jsonResponse = JsonSerializer.Serialize(inputObject);

另一个 OP 想要的关键是格式。如果您在序列化中添加格式选项,那将是令人惊叹的。 - Umang
虽然不完全是我所要求的,但AndrewE做得很好,正是我需要的。 - Michael Hunsaker
尝试通过TCP连接发送数据,然后接收连续的响应。以前从未在这个级别上做过任何事情,目前正在艰难地挣扎。 - Michael Hunsaker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接