简体   繁体   English

C#字符串插值 - 转义双引号和花括号

[英]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: 您似乎错过了productsquery对象的转义:

$@"{{
    ""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有两个原因:

  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?) 它更具可读性和可维护性(有人需要多长时间才能更改代码,因为json结构已经改变,同时保持所有转义顺序 - 特别是如果没有单元测试?)
  2. it's a lot more reliable (what if 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM