从JSON响应中选择并组合所有属于相同ID的元素作为子列表。

huangapple go评论81阅读模式
英文:

Select and combine all elements as sub list that belongs to the same ID from a JSON response

问题

Sure, here is the translated part of the content you provided:

**问题**

下面是一个JSON响应,其中包含3条记录。可以有多个属于相同ID的记录,例如ID **75029560**。我想获取属于该ID的一些数据作为子列表,然后如果有的话转移到另一个ID,请参阅下面的**期望输出**。

对于C#和LINQ,任何帮助都会很有帮助,我是一个C#和LINQ的新手。

**JSON响应**

{
    'list': [
        {
            'installation': {
                'id': 75029560,
                'identification': 'Bob',
                'name': 'Bob'
            },
            'timeStart': {
                'value': '2019-09-25T08:45:00'
            },
            'wattHourCumulative': 0,
            'wattHourPer15Min': 120,
            'year': 2019
        },
        {
            'installation': {
                'id': 78206452,
                'identification': 'Steve',
                'name': 'Steve'
            },
            'timeStart': {
                'value': '2019-09-25T09:00:00'
            },
            'wattHourCumulative': 0,
            'wattHourPer15Min': 400,
            'year': 2019
        },
        {
            'installation': {
                'id': 75029560,
                'identification': 'Bob',
                'name': 'Bob'
            },
            'timeStart': {
                'value': '2019-09-25T09:00:00'
            },
            'wattHourCumulative': 0,
            'wattHourPer15Min': 170,
            'year': 2019
        }
    ],
    'success': true,
    'totalNumberOfItems': 3
}

**期望输出**

{
    'list': [
        {
            'user': {
                'id': 75029560,
                'data': [{
                    'record': {
                        'wattHourCumulative': 0,
                        'wattHourPer15Min': 120
                    }
                }, {
                    'record': {
                        'wattHourCumulative': 0,
                        'wattHourPer15Min': 170
                    }
                }]
            }
        }, {
            'id': 78206452,
            'data': {
                'record': {
                    'wattHourCumulative': 0,
                    'wattHourPer15Min': 400
                }
            }
        }
    ]
}
英文:

Problem

Below I have a JSON response, in which there are 3 records. There can be multiple records belonging to the same ID, for instance, ID 75029560. I would like to fetch some data belonging to that ID as a sublist and then move to another ID if there is, please see the Desired Output below.

Any help would be great, I am a newbie to C# and LINQ.

JSON response

{
    'list': [
        {
            'installation': {
                'id': 75029560,
                'identification': 'Bob',
                'name': 'Bob'
            },
            'timeStart': {
                'value': '2019-09-25T08:45:00'
            },
            'wattHourCumulative': 0,
            'wattHourPer15Min': 120,
            'year': 2019
        },
        {
            'installation': {
                'id': 78206452,
                'identification': 'Steve',
                'name': 'Steve'
            },
            'timeStart': {
                'value': '2019-09-25T09:00:00'
            },
            'wattHourCumulative': 0,
            'wattHourPer15Min': 400,
            'year': 2019
        },
        {
            'installation': {
                'id': 75029560,
                'identification': 'Bob',
                'name': 'Bob'
            },
            'timeStart': {
                'value': '2019-09-25T09:00:00'
            },
            'wattHourCumulative': 0,
            'wattHourPer15Min': 170,
            'year': 2019
        }
    ],
    'success': true,
    'totalNumberOfItems': 3
}

Desired Output

{
    'list': [
        {
            'user': {
                'id': 75029560,
                'data': [{
                     'record': {
                          'wattHourCumulative':  0,
                          'wattHourPer15Min': 120
                      },
                     'record': {
                          'wattHourCumulative':  0,
                          'wattHourPer15Min': 170
                      }]
                 },
            },{
                'id': 78206452,
                'data': {
                     'record': {
                          'wattHourCumulative':  0,
                          'wattHourPer15Min': 400
                      }
                 }
            }
        }
    ]
}

答案1

得分: 1

你可以通过使用 Newtonsoft.Json.Linq 来实现所需的输出 - 解析响应为 JObject,按 id 分组并格式化结果。

var document = JObject.Parse(json);
var result = document["list"].GroupBy(item => item["installation"]?["id"],
	(id, items) => new
	{
		user = new
		{
			id,
			data = items.Select(i => new
			{
				wattHourCumulative = i["wattHourCumulative"]?.Value<int>(),
				wattHourPer15Min = i["wattHourPer15Min"]?.Value<int>()
			}).ToList()
		}
	});

var jsonObject = JObject.FromObject(new { list = result });
var resultJson = jsonObject.ToString();

请注意,你的输出格式不正确,data 属性应该是一个数组,因为你对多个记录进行了分组,list 数组应包含多个 user 项。

结果的 JSON 将如下所示:

{
    "list": [{
            "user": {
                "id": 75029560,
                "data": [{
                        "wattHourCumulative": 0,
                        "wattHourPer15Min": 120
                    }, {
                        "wattHourCumulative": 0,
                        "wattHourPer15Min": 170
                    }
                ]
            }
        }, {
            "user": {
                "id": 78206452,
                "data": [{
                        "wattHourCumulative": 0,
                        "wattHourPer15Min": 400
                    }
                ]
            }
        }
    ]
}
英文:

You can achieve the desired output by using Newtonsoft.Json.Linq - parse the response into JObject, group by id and format the result

var document = JObject.Parse(json);
var result = document[&quot;list&quot;].GroupBy(item =&gt; item[&quot;installation&quot;]?[&quot;id&quot;],
	(id, items) =&gt; new
	{
		user = new
		{
			id,
			data = items.Select(i =&gt; new
			{
				wattHourCumulative = i[&quot;wattHourCumulative&quot;]?.Value&lt;int&gt;(),
				wattHourPer15Min = i[&quot;wattHourPer15Min&quot;]?.Value&lt;int&gt;()
			}).ToList()
		}
	});

var jsonObject = JObject.FromObject(new { list = result });
var resultJson = jsonObject.ToString();

Please, keep in mind, that your output is incorrectly formatted, the data property should be an array, because you group a multiple records, list array should contain multiple user items.

The result JSON will be the following

{
    &quot;list&quot;: [{
            &quot;user&quot;: {
                &quot;id&quot;: 75029560,
                &quot;data&quot;: [{
                        &quot;wattHourCumulative&quot;: 0,
                        &quot;wattHourPer15Min&quot;: 120
                    }, {
                        &quot;wattHourCumulative&quot;: 0,
                        &quot;wattHourPer15Min&quot;: 170
                    }
                ]
            }
        }, {
            &quot;user&quot;: {
                &quot;id&quot;: 78206452,
                &quot;data&quot;: [{
                        &quot;wattHourCumulative&quot;: 0,
                        &quot;wattHourPer15Min&quot;: 400
                    }
                ]
            }
        }
    ]
}

huangapple
  • 本文由 发表于 2020年1月7日 00:51:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定