【问题标题】:C# Json LinQ - Build Json String Using LoopC# Json LinQ - 使用循环构建 Json 字符串
【发布时间】:2021-03-31 16:01:00
【问题描述】:

我正在尝试生成一个如下所示的 Json 字符串:

{
  "fromAddress": {
    "streetLines": [
      "100 Test St",
      "Ste 100"
    ],
    "city": "Orlando",
    "stateOrProvinceCode": "FL",
    "postalCode": "32819",
    "countryCode": "US"
  },
  "toAddress": {
    "streetLines": [
      "101 Test St",
      null
    ],
    "city": "Orlando",
    "stateOrProvinceCode": "FL",
    "postalCode": "32819",
    "countryCode": "US"
  },
  "packageDimensions": [
   {
      "weight": 20.0,
      "length": "18",
      "width": "12",
      "height": "13"
   },
   {
      "weight": 20.0,
      "length": "18",
      "width": "12",
      "height": "13"
   },
   {
      "weight": 20.0,
      "length": "18",
      "width": "12",
      "height": "13"
   }
   ]
}

我的代码已经做到了这一点:

        var json = new
            {
                fromAddress = new
                {
                    streetLines = new[]
                    {
                        fromAddress1,
                        fromAddress2
                    },
                    city = fromCity,
                    stateOrProvinceCode = fromState,
                    postalCode = fromZip,
                    countryCode = fromCountry
                },
                toAddress = new
                {
                    streetLines = new[]
                    {
                        toAddress1,
                        toAddress2
                    },
                    city = toCity,
                    stateOrProvinceCode = toState,
                    postalCode = toZip,
                    countryCode = toCountry
                },
            };

这会产生以下内容,但缺少包装尺寸:

{
  "fromAddress": {
    "streetLines": [
      "100 Test St",
      "Ste 100"
    ],
    "city": "Orlando",
    "stateOrProvinceCode": "FL",
    "postalCode": "32819",
    "countryCode": "US"
  },
  "toAddress": {
    "streetLines": [
      "101 Test St",
      null
    ],
    "city": "Orlando",
    "stateOrProvinceCode": "FL",
    "postalCode": "32819",
    "countryCode": "US"
  }
}

我的包装尺寸数据是这样存储的:

List<double> weight
string length
string width
string height

重量的多个值与列表一起传入,尺寸是常数。

我需要找到一种方法来添加包装尺寸,迭代重量列表来做到这一点。但我不确定如何处理它,在 Json 构建中添加循环无论如何都不起作用。

对于获得所需数据迭代的任何建议,我将不胜感激。谢谢。

【问题讨论】:

    标签: c# json linq


    【解决方案1】:

    您必须添加带有投影的 LINQ 表达式:

    var json = new
        {
            fromAddress = new
            {
                streetLines = new[]
                {
                    fromAddress1,
                    fromAddress2
                },
                city = fromCity,
                stateOrProvinceCode = fromState,
                postalCode = fromZip,
                countryCode = fromCountry
            },
            toAddress = new
            {
                streetLines = new[]
                {
                    toAddress1,
                    toAddress2
                },
                city = toCity,
                stateOrProvinceCode = toState,
                postalCode = toZip,
                countryCode = toCountry
            },
            packageDimensions = weight.Select(w => new 
            {
                weight = w,
                length = length,
                width = width,
                height = height
            })
            .ToArray()
        };
    

    【讨论】:

      猜你喜欢
      • 2017-12-14
      • 1970-01-01
      • 2019-01-03
      • 2012-01-14
      • 1970-01-01
      • 1970-01-01
      • 2015-12-08
      • 2012-06-28
      • 2017-03-06
      相关资源
      最近更新 更多