英文:
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["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();
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
{
"list": [{
"user": {
"id": 75029560,
"data": [{
"wattHourCumulative": 0,
"wattHourPer15Min": 120
}, {
"wattHourCumulative": 0,
"wattHourPer15Min": 170
}
]
}
}, {
"user": {
"id": 78206452,
"data": [{
"wattHourCumulative": 0,
"wattHourPer15Min": 400
}
]
}
}
]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论