linq 需要从JSON数据中获取值数组

icomxhvb  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(126)

我有一个API调用返回给我的JSON。我试图从一个名为“products”的属性中获取信息。一旦我将其放入数组或列表中,我需要查找一个特定的产品名称,然后查找它的一个属性。
我尝试了很多不同的方法,但都没有成功。我也在寻找,但没有在我的场景中找到我想要的东西。
如果我使用下面的代码,我可以将数据放入parseJson对象,但我不明白如何从那里将“产品”拉入数组,以便循环查找特定的产品及其值。

dynamic parseJson = JsonConvert.DeserializeObject(response.Content);

我也试过这个,但也没有运气。

dynamic parseJson = JsonConvert.DeserializeObject<AccurateApiResult>(response.Content);

public class AccurateApiResult 
    {
        public List<AccurateProduct> products { get; set; }
    }

public class AccurateProduct
    {
        public int id { get; set; }
        public string productType { get; set; }
        public string status { get; set; }
        public string result { get; set; }
        public bool flag { get; set; }
    }

下面是示例数据,我只对“产品”部分感兴趣。如何才能只提取这些数据?

[
    {
        "resource": "ORDER",
        "id": "Y10046727",
        "created": "2022-06-28T13:18:17Z",
        "updated": "2022-06-28T13:18:17Z",
        "workflow": "INTERACTIVE",
        "candidate": {
            "firstName": "Marcus",
            "lastName": "Willis",
            "middleName": null,
            "suffix": null,
            "dateOfBirth": "1990-01-01",
            "ssn": "111111111",
            "email": null,
            "phone": "240-5798551",
            "address": "3433 Lumar dr",
            "city": "Fort Washington",
            "region": "MD",
            "country": "US",
            "postalCode": "20744",
            "governmentId": {
                "country": "US",
                "type": null,
                "number": null
            },
            "aliases": [],
            "educations": [
                {
                    "school": "Test University",
                    "country": "US",
                    "region": "CA",
                    "city": "Irvine",
                    "degree": null,
                    "major": null,
                    "startDate": null,
                    "endDate": null,
                    "graduated": false,
                    "graduationDate": null,
                    "presentlyEnrolled": false
                }
            ],
            "prevEmployed": null,
            "employments": [],
            "convicted": null,
            "convictions": null,
            "references": [],
            "addressHistory": []
        },
        "completed": "2022-07-07T01:59:13Z",
        "supportReferenceId": "Y10046727",
        "status": "COMPLETE",
        "result": "Meets Requirements",
        "products": [
            {
                "id": 66134505,
                "productType": "AELS",
                "status": "COMPLETE",
                "result": "NOT APPLICABLE",
                "flag": false
            },
            {
                "id": 66134506,
                "productType": "ADJ",
                "status": "COMPLETE",
                "result": "NOT APPLICABLE",
                "flag": false
            },
            {
                "id": 66134508,
                "productType": "MOV",
                "status": "COMPLETE",
                "result": "Invalid SSN",
                "flag": false
            },
            {
                "id": 66144583,
                "productType": "MVR",
                "status": "COMPLETE",
                "result": "NOT APPLICABLE",
                "flag": false
            },
            {
                "id": 66144584,
                "productType": "F/M",
                "status": "COMPLETE",
                "result": "NO RECORD FOUND",
                "flag": false
            },
            {
                "id": 66144587,
                "productType": "EDU",
                "status": "COMPLETE",
                "result": "NOT APPLICABLE",
                "flag": false
            },
            {
                "id": 66144588,
                "productType": "DL5D",
                "status": "COMPLETE",
                "result": "Negative",
                "flag": false
            }
        ],
        "percentageComplete": 0,
        "candidateInfoChanged": false,
        "searchId": 66134503,
        "subjectId": 10121219,
        "requestor": "maegan@email.com"
    }
vm0i2vca

vm0i2vca1#

您可以尝试以下代码

var parsedJsonProducts = (JArray) JArray.Parse(response.Content)[0]["products"];

List<AccurateProduct> products = parsedJsonProducts.ToObject<List<AccurateProduct>>();

相关问题