如何通过 java 流获取响应的子对象

问题描述 投票:0回答:1

好吧,我一整天都在用头撞墙,无法解决这个问题。我试图在对象列表中找到一个 id,该列表类似于 java 流的层次结构中的 3 级。我知道如何用 for 循环来做到这一点,但我需要用流来获取它。 json 响应是

"NumberOfOwners": 1,
    "CurrentPage": 1,
    "TotalItems": 1,
    "TotalPages": 1,
    "PageSize": 1,
    "PageItems": [
        {
            "Id": 1560,
            "Title": "PlsWrk",
            "IsSubmitted": true,
            "Owner": {
                "Branch": null,
                "Position": null,
                "Id": null,
                "FirstName": null,
                "LastName": null,
                "ParentName": null,
                "Gender": null,
                "Image": null,
                "LoginStatusId": 0,
                "EmployeeStatus": 0,
                "CompanyName": null,
                "IsSubcontractor": false
            },
            "KeyResults": [
                {
                    "Id": 5032,
                    "Title": "asdf1",
                    "OverallStatus": 2,
                    "MonthKeyResults": [
                        {
                            "Id": 12484,
                            "Month": 9,
                            "Status": 3,
                            "Progress": "REaplace1"
                        },
                        {
                            "Id": 12483,
                            "Month": 8,
                            "Status": 3,
                            "Progress": "sadf4"
                        },
                        {
                            "Id": 12482,
                            "Month": 7,
                            "Status": 1,
                            "Progress": "On Track1"
                        }
                    ]
                },
                {
                    "Id": 5033,
                    "Title": "asdf2",
                    "OverallStatus": 1,
                    "MonthKeyResults": [
                        {
                            "Id": 12485,
                            "Month": 7,
                            "Status": 2,
                            "Progress": "Recovery2"
                        },
                        {
                            "Id": 12487,
                            "Month": 9,
                            "Status": 2,
                            "Progress": "asdfreas"
                        },
                        {
                            "Id": 12486,
                            "Month": 8,
                            "Status": 1,
                            "Progress": "asdf5"
                        }
                    ]
                },
                {
                    "Id": 5034,
                    "Title": "asdf3",
                    "OverallStatus": 2,
                    "MonthKeyResults": [
                        {
                            "Id": 12490,
                            "Month": 9,
                            "Status": 1,
                            "Progress": "asdafa"
                        },
                        {
                            "Id": 12489,
                            "Month": 8,
                            "Status": 2,
                            "Progress": "asdf6"
                        },
                        {
                            "Id": 12488,
                            "Month": 7,
                            "Status": 3,
                            "Progress": "Replace3"
                        }
                    ]
                }
            ]
        }
    ]

确切地说,我希望流返回具有特定 id 的 MonthyKeyResult 对象 自动取款机我在这里

public static MonthKeyResults getOkrMonthlyProgressById(PageItems okr, Integer monthlyProgressId){

        //here i get KeyResults object list
        List<KeyResults> keyResult = okr.getKeyResults().stream().collect(Collectors.toList());
            
            //and now I am trying to get all MonthKeyResults 
            //objects but I not doing it right
            List<MonthKeyResults> monthKeyResults = keyResult.stream().
                       filter(monthKeyResult -> monthKeyResult.
                                  getMonthKeyResults().
                                  stream().collect(Collectors.toList()));
            
            //and then I am thinking of going trough the monthKeyResults 
            //list with stream and finding Id I need and returning that 
            //whole object with something like this
    MonthKeyResults mKeyResults = monthKeyResults.stream().filter(id -> id.getId().
                    equals(monthlyProgressId)).findAny().orElse(null);
    return mKeyResult
}
我还有一个问题 我已经将最终对象分开,如您在 3 个流中看到的那样,是否可以一次性获取它,或者您需要像这样分开这些对象并分别遍历它们?

java json stream find response
1个回答
2
投票

您可能正在寻找类似的东西:

return okr.getKeyResults().stream()
        .flatMap(keyResult -> keyResult.getMonthKeyResults().stream())
        .filter(monthKeyResult -> monthKeyResult.getId().equals(monthlyProgressId))
        .findAny()
        .orElse(null);
© www.soinside.com 2019 - 2024. All rights reserved.