C# 获取包含特定属性值的jToken列表

C# 获取包含特定属性值的jToken列表,c#,linq,json.net,C#,Linq,Json.net,我有一个json: { "IsSuccess": true, "Message": "Success", "ResponseData": [ { "ContentID": null, "ProductVersionEvritChildren": "0.22", "BookVisualsVersion": "0.17", "iconDescriptionPage": "Images/Products/covers_2018/l

我有一个json:

{
"IsSuccess": true,
"Message": "Success",
"ResponseData": [
    {
        "ContentID": null,
        "ProductVersionEvritChildren": "0.22",
        "BookVisualsVersion": "0.17",
        "iconDescriptionPage": "Images/Products/covers_2018/lionTutIconPng.png",
        "IsBetaOnly": false,
        "ProductType": 8,
        "ProductID": 8337

    },
    {
        "ContentID": null,
        "ProductVersionEvritChildren": "0.12",
        "BookVisualsVersion": "0.12",
        "iconDescriptionPage": "Images/Products/covers_2018/BialikPngIcon.png",
        "IsBetaOnly": false,
        "ProductType": 8,
        "ProductID": 8352

    },
    {
        "ContentID": null,
        "ProductVersionEvritChildren": "0.13",
        "BookVisualsVersion": "0.12",
        "iconDescriptionPage": "Images/Products/covers_2018/ariotPngIcon.png",
        "IsBetaOnly": false,
        "ProductType": 8,
        "ProductID": 8342

    },
    {
        "ContentID": null,
        "ProductVersionEvritChildren": "0.14",
        "BookVisualsVersion": "0.12",
        "iconDescriptionPage": "Images/Products/covers_2018/magicHatPngIcon.png",
        "IsBetaOnly": false,
        "ProductType": 8,
        "ProductID": 8343

    },
    {
        "ContentID": null,
        "ProductVersionEvritChildren": "0.11",
        "BookVisualsVersion": "0.12",
        "iconDescriptionPage": "Images/Products/covers_2018/littleMichalPngIcon.png",
        "IsBetaOnly": false,
        "ProductType": 8,
        "ProductID": 8347
    }
]
}

我有一个给定的ProductID列表:

  private static readonly List<string> FreeBooksProductId = new List<string>()
    {
        "8352",
        "8347"
    };
private静态只读列表FreeBooksProductId=new List()
{
"8352",
"8347"
};
我想得到一个jtonken列表,其中包含我在列表中的ProductID


我知道有一个SelectTokens方法可以提供我要查找的内容,但我不知道当我需要检查列表时如何使用它。

为什么不将发布的JSON反序列化为类下模式,并使用LINQ查询来获得所需的数据。像

public class ResponseData
{
    public object ContentID { get; set; }
    public string ProductVersionEvritChildren { get; set; }
    public string BookVisualsVersion { get; set; }
    public string iconDescriptionPage { get; set; }
    public bool IsBetaOnly { get; set; }
    public int ProductType { get; set; }
    public int ProductID { get; set; }
}

public class RootObject
{
    public bool IsSuccess { get; set; }
    public string Message { get; set; }
    public List<ResponseData> ResponseData { get; set; }
}

var data = JsonConvert.DeserializeObject<RootObject>(jsonString);

data.ResponseData.Where(r => FreeBooksProductId.Contains(r.ProductID)).ToList();
公共类响应数据
{
公共对象ContentID{get;set;}
公共字符串PRODUCTVERSIONEVRICHILDREN{get;set;}
公共字符串版本{get;set;}
公共字符串iconDescriptionPage{get;set;}
公共bool只有{get;set;}
public int ProductType{get;set;}
public int ProductID{get;set;}
}
公共类根对象
{
公共bool issucess{get;set;}
公共字符串消息{get;set;}
公共列表响应数据{get;set;}
}
var data=JsonConvert.DeserializeObject(jsonString);
data.ResponseData.Where(r=>FreeBooksProductId.Contains(r.ProductID)).ToList();

尽管将
JSON
反序列化为对象更好,但是您仍然可以使用前面提到的方法

假设
json
是包含发布的
json
string
变量,您可以使用以下代码段:

JObject parsedJson =  JObject.Parse(json);
List<JToken> listOfDesiredTokens = parsedJson.SelectTokens("ResponseData").Children()
                                             .Where(x => FreeBooksProductId.Contains((string)x.SelectToken("ProductID")))
                                             .ToList();
JObject-parsedJson=JObject.Parse(json);
List listOfDesiredTokens=parsedJson.SelectTokens(“ResponseData”).Children()
.Where(x=>FreeBooksProductId.Contains((字符串)x.SelectToken(“ProductID”))
.ToList();

您将获得一个
JToken
列表,其中产品id包含在
FreeBooksProductId
列表中。

在linq中使用where with contains。我也需要把你的答案反序列化。但无论如何,我都在反序列化JSON,而且在反序列化JSON时更容易操作它的数据。