JAVA:如何为复杂的Json制作自定义内部类

时间:2017-03-14 13:12:32

标签: java json jackson

我已经阅读了很多帖子,但是没有找到如何为这个json制作课程。

{
    "snippet": {
        "parentGroupId": "69ea5920-0157-1000-0000-0000028e1b90",
        "processors": {},
        "processGroups": {
            "1231-23a": {
                "clientId": "50b3ec1a-c123-1e4f-718c-b0323fb1e175",
                "version": 0
            }
        }
    }
}

问题是属性“1231-23a”可以像我这样在我的json中改变:

{
    "snippet": {
        "parentGroupId": "69ea5920-0157-1000-0000-0000028e1b90",
        "processors": {},
        "processGroups": {
            "4544-412f": {
                "clientId": "50b3ec1a-c123-1e4f-718c-b0323fb1e175",
                "version": 0
            }
        }
    }
}

感谢你的帮助

2 个答案:

答案 0 :(得分:1)

您可以使用Map中的String来嵌套数据,例如ProcessGroup。然后"1231-23a"和“4544-412f”将成为此Map中的关键字。例如,"snippet""processGroups"的这些类,并添加构造函数,getter等

class Snippet {
    String parentGroupId;
    Processors processors;
    Map<String, ProcessGroup> processGroups;
}


class ProcessGroup {
    String clientId;
    int version;
}

答案 1 :(得分:0)

@manos,

我班上缺少正常工作。

<强>段

class Snippet {
    String parentGroupId;
//    Processors processors;
    Map<String, ProcessGroup> processGroups;

    public void setParentGroupId(String string) {

        this.parentGroupId = string ;

    }

    public void setProcessGroups(Map<String, ProcessGroup> procGps)
    {
        this.processGroups = procGps;
    }

    public String toString()
    {
        String str = "{\n"
                + "   \"snippet\": {\n"
                +"     \"parentGroupId\": \""+ this.parentGroupId      +"\",\n"
                +"     \"processGroups\": \""+ this.processGroups +"\""
                + "\n}";
        return str;
    }

}

ProcessGroup类

class ProcessGroup {
    String clientId;
    int version;

    public void setClientId(String clientId) {
        this.clientId = clientId; 

    }

    public void setVersion(int version) {
        this.version = version;

    }

    public String toString()
    {
        String str = "\n    \"clientId\": \"" + this.clientId +"\"\n "
                    +"  \"version\": "+ this.version;
        return str;
    }
}

测试类

public class Test {

       public static void main(String[] args) {

            Map<String, ProcessGroup> map = new HashMap<String, ProcessGroup>();


           // ObjectMapper oMapper = new ObjectMapper();

            //Student obj = new Student();
            Snippet obj = new Snippet();
            ProcessGroup objPG = new ProcessGroup();

            objPG.setClientId("clientId-10");
            objPG.setVersion(12);

            obj.setParentGroupId("parentId-10");

            map.put("processGroup-10", objPG);

            obj.setProcessGroups(map);

            // object -> Map

            System.out.println(obj);

        }

}

<强>结果

好像,我似乎无法访问&#39;处理Group-10并加上符号&#34; =&#34;而不是&#34;:&#34; 怎么了?

{
 "snippet": {
  "parentGroupId": "parentId-10",
  "processGroups": "{processGroup-10=
 "clientId": "clientId-10"
 "version": 12}"
}