c# - 使用 LINQ 提取 JSON 属性
问题描述
我有一个 json 对象,如下所示:
{
"search-results": {
"results-count": "2"
"entry": [
{
"id": "0",
"count": "10",
},
{
"id": "1",
"count": "22",
}
]
}
}
我想得到count
s 的总和;32
在这个例子中。我正在考虑使用 LINQ 执行此操作,但任何更“可读”的替代方案将不胜感激。
解决方案
试试这个:
var json = @"{
'search-results': {
'results-count': '2',
'entry': [
{
'id': 0,
'count': 10,
},
{
'id': 1,
'count': 22,
}
]
}
}";
JObject searchResult = JObject.Parse(json);
int sum = (from entry in searchResult["search-results"]["entry"]
select (int)entry["count"]).Sum();
推荐阅读
- google-apps-script - 为什么我的 GOOGLEFINANCE 公式的结果总是一样的?
- javascript - Firebase JavaScript 如何将数据库中的所有字段显示到 HTML 文件中
- excel - 3 种颜色代码 - 使用 pandas 条件格式化 Excel 单元格
- excel - 如何使用 VBA 确定将数据从一个工作表粘贴到另一个工作表的位置?
- postgresql - 使用 postgreSQL 更新每一行中的特定列
- python - Code fail with name is not defined 使用 pydev 运行的错误,但从终端运行良好
- azure - Azure Functions 的 AKS(Azure Kubernetes 服务)Pod 冷启动
- python - 从 Python 脚本调用 SQL 5.6 例程
- android - 如何从其他页面获取文本输入详细信息
- javascript - 使用 await 更新 Mongoose 密码