【问题标题】:How to escape Multi Line string如何转义多行字符串
【发布时间】:2019-08-02 16:23:43
【问题描述】:

我有密码

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

我不能用\来转义它,因为我有修饰符@,而且它在双引号中,所以我不能用更多的引号来转义它。

我想让 EMAIL 成为一个可以更改的变量,而不是字符串文本的一部分。

【问题讨论】:

  • 您能否更具体地说明您想要做什么以及什么不起作用?
  • @LukedukeAnimations 检查我的答案并标记是否有帮助,谢谢 :)

标签: c# string paypal-sandbox


【解决方案1】:

只需在您需要创建变量的部分打破@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""
                }
              ]
            }";

【讨论】:

  • 如果emailVar 来自不受信任的来源,将其包含在这样的字符串中可用于代码注入攻击。因此,在使用它手动构建 JSON 之前,请在字符串上使用像 HttpUtility.JavaScriptStringEncode 这样的转义函数。
【解决方案2】:

不要直接构建json字符串,而是使用C# object literal notation先将数据结构创建为.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);

另见

【讨论】:

  • 这是一个非常干净的方法。就我而言,我需要一个同样受支持的数组:string json = JsonConvert.SerializeObject( new[] { new { op = "replace", path = "/title", value = newTitle } }
猜你喜欢
  • 2018-02-25
  • 2013-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-08
  • 1970-01-01
  • 2020-04-26
相关资源
最近更新 更多