【发布时间】: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 构建中添加循环无论如何都不起作用。
对于获得所需数据迭代的任何建议,我将不胜感激。谢谢。
【问题讨论】: