C#字符串插值 - 划船双引号和卷发括号

发布于 2025-01-24 09:51:48 字数 1756 浏览 0 评论 0原文

伙计们,

我正在通过插值字符串构建一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幸福%小乖 2025-01-31 09:51:48

看来您错过了products查询对象的逃脱:

$@"{{
    ""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
}}";

It seems that you have missed escape for the products and query objects:

$@"{{
    ""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
}}";
脱离于你 2025-01-31 09:51:48

以防万一其他人正在考虑这样做,最好创建匿名类型并将其序列化与JSON序列化,原因有两个:

  1. 它更可读取和可维护(因为JSON,需要多长时间更改代码需要多长时间才能更改代码。结构在保持所有逃脱的顺序时发生了变化 - 尤其是在没有单元测试的情况下?
  2. 更为可靠(如果taskname)。

)下面使用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);

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:

  1. it's a lot more readable and maintainable (how long would it take someone to change the code because the json structure has changed while keeping all escapes in order -- especially if there are no unit tests?)
  2. it's a lot more reliable (what if taskName has a double quote?)

Below uses json.net for serialization.

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);
命比纸薄 2025-01-31 09:51:48

@“ ...”$“ ...” 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
}}";

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:

$@"{{
    ""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
}}";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文