如何转义多行字符串
How to escape Multi Line string
我有密码
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 成为一个可以更改的变量,而不是字符串文本的一部分。
只需在需要创建变量的部分打断@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# 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);
另见
- How do I turn a C# object into a JSON string in .NET?
- Issues creating object literal using anonymous types in c#
我有密码
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 成为一个可以更改的变量,而不是字符串文本的一部分。
只需在需要创建变量的部分打断@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# 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);
另见
- How do I turn a C# object into a JSON string in .NET?
- Issues creating object literal using anonymous types in c#