Java 如何对API响应进行分组和排序

Java 如何对API响应进行分组和排序,java,api,jackson,Java,Api,Jackson,我的API非常通用,它旨在提供多种不同的体验。在我的Java控制器中,我调用以获取数据,并使用Jackson树读取数据。我需要做的是按照我希望在UI中显示的方式对结果进行分组和排序。在回复回来之前,我会知道我想要的排序和分组,那么最好的方法是什么呢 下面是我返回的JSON的一般示例: { "callerId": "1234567890", "data": { "type": "APIResponseObject", "dataObj": [

我的API非常通用,它旨在提供多种不同的体验。在我的Java控制器中,我调用以获取数据,并使用Jackson树读取数据。我需要做的是按照我希望在UI中显示的方式对结果进行分组和排序。在回复回来之前,我会知道我想要的排序和分组,那么最好的方法是什么呢

下面是我返回的JSON的一般示例:

{
    "callerId": "1234567890",
    "data": {
        "type": "APIResponseObject",
        "dataObj": [
            {
                "APICode": "MY_API_CODE",
                "dataCategories": [
                    {
                        "label": "Generic Label 5",
                        "code": "ITEM_CODE_5"
                    },
                    {
                        "label": "Generic Label 4",
                        "code": "ITEM_CODE_4"
                    },
                    {
                        "label": "Generic Label 3",
                        "code": "ITEM_CODE_3"
                    },
                    {
                        "label": "Generic Label 2",
                        "code": "ITEM_CODE_2"
                    },
                    {
                        "label": "Generic Label 1",
                        "code": "ITEM_CODE_1"
                    }
                ]
            }
        ]
    }
}
下面是我希望JSON返回到UI时的外观示例:

{
    "callerId": "1234567890",
    "data": {
        "type": "APIResponseObject",
        "dataObj": [
            {
                "APICode": "MY_API_CODE",
                "dataCategories": [
                    {
                        "group1":[
                        {
                            "label": "Generic Label 1",
                            "code": "ITEM_CODE_1"
                        },
                        {
                            "label": "Generic Label 2",
                            "code": "ITEM_CODE_2"
                        },
                        ]
                    },
                    "group2":[
                        {
                            "label": "Generic Label 3",
                            "code": "ITEM_CODE_3"
                        },
                        {
                            "label": "Generic Label 4",
                            "code": "ITEM_CODE_4"
                        },
                        {
                            "label": "Generic Label 5",
                            "code": "ITEM_CODE_5"
                        }
                    ]
                ]
            }
        ]
    }
}
这是我的RestController中的一段代码,我使用Jackson读取它并将其存储在JsonNode中:

    //read json file data to String
    byte[] jsonData = Files.readAllBytes(path);

    //create ObjectMapper instance
    ObjectMapper objectMapper = new ObjectMapper();

    //convert json string to object
    MyObject data = objectMapper.readValue(jsonData, MyObject.class);

    // re-order the response based on a template of some kind
    // Look into @JsonPropertyOrder...which allows us to specify
    // the order of properties (may be on serialization only)

    // parse json into JsonNode so we can use tree model
    JsonNode rootNode = objectMapper.readTree(jsonData);
然后我会将该根节点传递到其他类中,如:

// Now pass rootNode into some class that returns the JSON
    // in the correct order that I want. Something like 
    // *TransformJsonByType*. That accepts JSON in one form and returns
    // it in the form that I want. 

    public class TransformJsonByType{
       // Accepts JSON in one format and spits it out in another using 
       // Jackson Tree Traversal
    }
我的波乔很好。我想我唯一需要弄清楚的就是这个MyObjectParser类

所以我知道我想把项目代码1和项目代码2分组在一起并首先显示,而项目3、4和5也分组在一起并最后显示

我不一定要寻找一个完整的解决方案,但我确实需要一个起点。如何在将JSON响应返回到UI之前对其进行变异?此外,我如何确保它符合这些规则


我是一名Java程序员新手,在这个领域刚刚起步。谢谢你的建议

您可以尝试在MyObject实现中添加定制的@JsonGetter

@JsonGetter("dataCatagories")
public List<String, List<ChildObj>> getDataCategoriesJSON() {
    List<String, List<ChildObj>>  json = new ArrayList<>();
    for (ChildObj co : this.dataCategories) {
        // TODO: whatever logic there
    }
}

这可能会解决问题,但这是最糟糕的解决方案。您不应该在放入POJO之前用正确的结构和数据定义POJO。

为什么要移动它?我不想把它放在这里,因为这个问题是开放式的。最好先把创建json的小组放在这里。你能分享一下你构建json的代码吗?我已经包括了上面的片段,我正在使用Jackson检索json响应。我认为我的POJO没有任何问题。我认为缺少的部分是我上面提到的MyObjectParser类,它接受原始JSON并按照我需要的方式对其进行修改。