C#字符串插值 - 划船双引号和卷发括号
伙计们,
我正在通过插值字符串构建一个JSON对象,而无法获得逃脱的工作方式。我必须为API使用双引号。
这不会插入卷曲括号之间的表达:
@"{{
""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
}}";
这很明显,卷曲括号没有正确逃脱:
$"{{
""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
}}";
我应该如何格式化它以保持内部双引号和外部托架,同时允许值在单括号内被插值?
guys,
I'm building a JSON object from an interpolated string, and not getting how escaping works. I have to use double quotes for the 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看来您错过了
products
和查询
对象的逃脱:It seems that you have missed escape for the
products
andquery
objects:以防万一其他人正在考虑这样做,最好创建匿名类型并将其序列化与JSON序列化,原因有两个:
taskname
)。)下面使用JSON.NET进行序列化
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:
taskName
has a double quote?)Below uses json.net for serialization.
除
@“ ...”
和$“ ...”
c#支持$@“ ...”
字符串当您构建需要插值的多行字符串文字时,您正在寻找的内容: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: