c# - 按给定值选择 json 标记

标签 c# json json.net

以下是我从服务器收到的json部分:

[
{
    "outcome_id": "4",
    "items": [
        {
            "outcome_item_id": "4",
            "outcome_sport_id": "311"
        }
    ]
},
{
    "outcome_id": "4",
    "items": [
        {
            "outcome_item_id": "4",
            "outcome_sport_id": "312"
        }
    ]
},
{
    "outcome_id": "5",
    "items": [
        {
            "outcome_item_id": "4",
            "outcome_sport_id": "313"
        }
    ]
}
]

我正在使用 Newtonsoft.Json.Linq 并且想知道是否有办法选择所有带有 outcome_id = 4 的标记?我希望我的问题足够清楚。因为我找不到任何合适的解决方案来满足我的需求。

提前致谢。

最佳答案

您可以将模型类创建为已知类型并反序列化 json。然后使用 Linq 简单地应用 Where 子句和 Select;

public class Item
{
    public string outcome_item_id { get; set; }
    public string outcome_sport_id { get; set; }
}

public class JsonObject
{
    public string outcome_id { get; set; }
    public List<Item> items { get; set; }
}

var data = JsonConvert.DeserializeObject<List<JsonObject>>(json);
var outcome_ids = data.Where(x => x.outcome_id == "4").Select(x => x.outcome_id);

关于c# - 按给定值选择 json 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48320490/

相关文章:

c# - 尝试从 setpixel/getpixel 进行优化

c# - 使用 Parallel.ForEach() 进行长时间运行的进程

ios - 如何让 Swift/Xcode 控制台显示中文字符而不是 unicode?

javascript - 从 jstree 中的外部 json 文件加载数据

c# - JSON.Net Xml 序列化误解了数组

c# - 为 strcmp() 上的溢出传递参数

c# - 从 AD 获取经理的员工

json - 从 KendoUI TreeView 获取当前数据

c# - JSON.net:如何在不使用默认构造函数的情况下反序列化?

asp.net - Json.Net 如何将 null 反序列化为空字符串?