获取包含特定属性值的jtoken列表

时间:2018-11-06 09:45:58

标签: c# linq json.net

我有这个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"
    };

,我想获取一个包含我在列表中包含的ProductID的jtonken列表。

我知道有一个SelectTokens方法可以提供所需的内容,但是当我需要与列表进行核对时,我不知道如何使用它。

2 个答案:

答案 0 :(得分:1)

为什么不将发布的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();

答案 1 :(得分:0)

尽管最好将from scipy.stats import rankdata from heapq import nlargest df = pd.concat([df]*100, ignore_index=True) %timeit df.mask(df.rank(axis=1, method='min', ascending=False) > 2, 0) # 2.23 ms per loop %timeit df.where(np.apply_along_axis(rankdata, 1, df, method='max') > 2, 0) # 45 ms per loop %timeit df.where(df.apply(lambda x: x.isin(nlargest(2, x)), axis=1), 0) # 92.4 ms per loop %timeit df.mask(~df.apply(lambda x: x.isin(x.nlargest(2)), axis=1), 0) # 274 ms per loop 反序列化为一个对象,但是您仍然可以使用您提到的SelectTokens方法。

假设JSON是一个json变量,其中包含您发布的string,则可以使用以下代码段:

JSON

您将获得JObject parsedJson = JObject.Parse(json); List<JToken> listOfDesiredTokens = parsedJson.SelectTokens("ResponseData").Children() .Where(x => FreeBooksProductId.Contains((string)x.SelectToken("ProductID"))) .ToList(); 列表,其中产品ID包含在JToken列表中。