[英]C# string interpolation-escaping double quotes and curly braces
guys, 伙计们,
I'm building a JSON object from an interpolated string, and not getting how escaping works. 我正在从插值字符串构建一个JSON对象,而不是获取转义的工作原理。 I have to use double quotes for the API.
我必须为API使用双引号。
This does not interpolate the expressions between the curly braces: 这不会插入花括号之间的表达式:
@"{{
""name"":""{taskName}"",
""products"": [
{""product"": ""ndvi_image"", ""actions"": [""mapbox"", ""processed""]},
{""product"": ""true_color"", ""actions"": [""mapbox"", ""processed""]}
],
""recurring"":true,
""query"": {
""date_from"": ""{dateFromString}"",
""date_to"": ""{dateToString}"",
""aoi"": {polygon}
},
""aoi_coverage_percentage"":90
}}";
This throws a bunch of errors-apparently, the curly brackets are not being escaped properly: 这会抛出一堆错误 - 显然,大括号没有正确转义:
$"{{
""name"":""{taskName}"",
""products"": [
{""product"": ""ndvi_image"", ""actions"": [""mapbox"", ""processed""]},
{""product"": ""true_color"", ""actions"": [""mapbox"", ""processed""]}
],
""recurring"":true,
""query"": {
""date_from"": ""{dateFromString}"",
""date_to"": ""{dateToString}"",
""aoi"": {polygon}
},
""aoi_coverage_percentage"":90
}}";
How should I format it in order to preserve the internal double quotes and outer brackets while allowing for the values inside the single brackets to be interpolated? 我应该如何格式化它以保留内部双引号和外括号,同时允许插入单个括号内的值?
It seems that you have missed escape for the products
and query
objects: 您似乎错过了
products
和query
对象的转义:
$@"{{
""name"":""{taskName}"",
""products"": [
{{""product"": ""ndvi_image"", ""actions"": [""mapbox"", ""processed""]}},
{{""product"": ""true_color"", ""actions"": [""mapbox"", ""processed""]}}
],
""recurring"":true,
""query"": {{
""date_from"": ""{dateFromString}"",
""date_to"": ""{dateToString}"",
""aoi"": {polygon}
}},
""aoi_coverage_percentage"":90
}}";
In addition to @"..."
and $"..."
C# supports $@"..."
strings, which is what you are looking for when you build multi-line string literals that need to be interpolated: 除了
@"..."
和$"..."
C#支持$@"..."
字符串,这是您在构建需要插值的多行字符串文字时所寻找的:
$@"{{
""name"":""{taskName}"",
""products"": [
{{""product"": ""ndvi_image"", ""actions"": [""mapbox"", ""processed""]}},
{{""product"": ""true_color"", ""actions"": [""mapbox"", ""processed""]}}
],
""recurring"":true,
""query"": {{
""date_from"": ""{dateFromString}"",
""date_to"": ""{dateToString}"",
""aoi"": {polygon}
}},
""aoi_coverage_percentage"":90
}}";
Just in case someone else is considering doing the same, it would be better to create an anonymous type and serialize it to json for two reasons: 为了防止其他人正在考虑做同样的事情,最好创建一个匿名类型并将其序列化为json有两个原因:
taskName
has a double quote?) taskName
有双引号怎么办?) Below uses json.net for serialization. 下面使用json.net进行序列化。
var jsonObj = new {
name = taskName,
products = new[] {
new { product = "ndvi_image", actions = new [] { new { mapbox = "processed" } },
new { product = "true_color", actions = new [] { new { mapbox = "processed" } }
},
recurring = true,
query = new {
date_from = dateFromString,
date_to = dateToString,
aoi = polygon
},
aoi_coverage_percentage = 90
};
var jsonString = JsonConvert.SerializeObject(jsonObj);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.