如何逃脱多线字符串



我有代码

var body = @"{
                  ""sender_batch_header"": {
                    ""email_subject"": ""You have a payment"",
                    ""sender_batch_id"": ""batch-1564759643870""
                  },
                  ""items"": [
                    {
                      ""recipient_type"": ""PHONE"",
                      ""amount"": {
                        ""value"": ""1.00"",
                        ""currency"": ""USD""
                      },
                      ""receiver"": ""4087811638"",
                      ""note"": ""Payouts sample transaction"",
                      ""sender_item_id"": ""item-1-1564759643870""
                    },
                    {
                      ""recipient_type"": ""EMAIL"",
                      ""amount"": {
                        ""value"": ""1.00"",
                        ""currency"": ""USD""
                      },
                      ""receiver"": ""ps-rec@paypal.com"",
                      ""note"": ""Payouts sample transaction"",
                      ""sender_item_id"": ""item-2-1564759643870""
                    },
                    {
                      ""recipient_type"": ""PAYPAL_ID"",
                      ""amount"": {
                        ""value"": ""1.00"",
                        ""currency"": ""USD""
                      },
                      ""receiver"": ""FSMRBANCV8PSG"",
                      ""note"": ""Payouts sample transaction"",
                      ""sender_item_id"": ""item-3-1564759643871""
                    }
                  ]
                }";

我希望收件人的电子邮件是字符串/变量,因此我需要逃脱双引号,但是我没有尝试过在线工作。这可能吗?此代码来自https://www.paypal.com/apex/product-profile/payouts/createpayouts

我无法使用逃脱它,因为我有修改器 @,并且它以双引号为单,所以我无法用更多的引号逃脱它。

我想使电子邮件成为可以更改的变量,而不是字符串文本的一部分。

只需在您需要创建一个变量的零件上打破@string即可。然后添加变量后,使用@string

再次启动字符串
var emailVar = ""; //email input
var body = @"{
              ""sender_batch_header"": {
                ""email_subject"": ""You have a payment"",
                ""sender_batch_id"": ""batch-1564759643870""
              },
              ""items"": [
                {
                  ""recipient_type"": ""PHONE"",
                  ""amount"": {
                    ""value"": ""1.00"",
                    ""currency"": ""USD""
                  },
                  ""receiver"": ""4087811638"",
                  ""note"": ""Payouts sample transaction"",
                  ""sender_item_id"": ""item-1-1564759643870""
                },
                {
                  ""recipient_type"": " + """ + emailVar + """ + @",
                  ""amount"": {
                    ""value"": ""1.00"",
                    ""currency"": ""USD""
                  },
                  ""receiver"": ""ps-rec@paypal.com"",
                  ""note"": ""Payouts sample transaction"",
                  ""sender_item_id"": ""item-2-1564759643870""
                },
                {
                  ""recipient_type"": ""PAYPAL_ID"",
                  ""amount"": {
                    ""value"": ""1.00"",
                    ""currency"": ""USD""
                  },
                  ""receiver"": ""FSMRBANCV8PSG"",
                  ""note"": ""Payouts sample transaction"",
                  ""sender_item_id"": ""item-3-1564759643871""
                }
              ]
            }";

,而不是直接构建JSON字符串,而是使用C#对象文字符号首先创建数据结构作为.NET对象,然后让.NET为您进行编码。

string eMailAddress = "someone@somewhere.net";
// Create the data using object literal notation in C#
var data = new
{
    sender_batch_header = new
    {
        email_subject = "You have a payment",
        sender_batch_id = "batch-1564759643870"
    },
    items = new[]
    {
        new
        {
            recipient_type = "PHONE",
            amount = new
            {
                value = 1,
                currency = "USD"
            },
            receiver = "4087811638",
            note = "Payouts sample transaction",
            sender_item_id = "item-1-1564759643870"
        },
        new
        {
            recipient_type = "EMAIL",
            amount = new
            {
                value = 1,
                currency = "USD"
            },
            receiver = eMailAddress,
            note = "Payouts sample transaction",
            sender_item_id = "item-2-1564759643870"
        }
    }
};
// Let .net translate it to json either using JavaScriptSerializer (You have to reference system.web.extensions)
string json = new JavaScriptSerializer().Serialize(data);
// or you could use JSON.net from NewtonSoft
// string json = JsonConvert.SerializeObject(data, Formatting.Indented);
MessageBox.Show(json);

另请参见

  • 如何将C#对象变成.net中的JSON字符串?
  • 在C#
  • 中使用匿名类型创建对象的问题

相关内容

  • 没有找到相关文章

最新更新