繁体   English   中英

如何使用JsonObject在Java中创建正确的Json

[英]How to create correct Json in Java using JsonObject

我如何使用JSONObject在Java中创建如下所示的JSON对象?

{
    "fields": {
     "issuetype":{"id": "10004"},
     "project":{"key": "TES"},
     "reporter":{"name":"TestUser"},
     "summary":"Screen not responding",
     "description":"New Bug in UI. Screen not responding",
     "assignee":{"name":"Test"}

     }
}

到目前为止我尝试过的

JsonObject issuetype = new JsonObject();
        issuetype.addProperty("id", "10004");
        JsonObject project = new JsonObject();
        project.addProperty("key", "TES");
        JsonObject reporter = new JsonObject();
        reporter.addProperty("name", "TestUser");
        JsonObject summary = new JsonObject();
        summary.addProperty("summary", "Screen not responding");
        JsonObject description = new JsonObject();
        description.addProperty("description", "New Bug in UI. Screen not responding");
        JsonObject assignee = new JsonObject();
        assignee.add("name", "Test");

有人可以帮我解决这个问题吗?

谢谢

您应该使用Json工厂类来创建对象构建器

JsonObject issuetype = Json.createObjectBuilder()
    .add("fields", Json.createObjectBuilder()
        .add("issuetype", Json.createObjectBuilder().add("id", "10004"))
        .add("project", Json.createObjectBuilder().add("key", "TES"))
        .add("reporter", Json.createObjectBuilder().add("name", "TestUser"))
        .add("summary", "Screen not responding")
        .add("description", "New Bug in UI. Screen not responding")
        .add("assignee", Json.createObjectBuilder().add("name", "Test"))
    )
    .build();

尝试跟随,

        JsonObject issuetype = new JsonObject();
        issuetype.addProperty("id", "10004");
        JsonObject project = new JsonObject();
        project.addProperty("key", "TES");
        JsonObject reporter = new JsonObject();
        reporter.addProperty("name", "TestUser");
        JsonObject summary = new JsonObject();
        summary.addProperty("summary", "Screen not responding");
        JsonObject description = new JsonObject();
        description.addProperty("description", "New Bug in UI. Screen not responding");
        JsonObject assignee = new JsonObject();
        assignee.addProperty("name", "Test");

        JsonObject field = new JsonObject();
        field.add("issuetype", issuetype);
        field.add("project", project);
        field.add("reporter", reporter);
     // field.add("summary", summary);
        field.addProperty("summary", "Screen not responding");
        field.add("description", description);
        field.add("assignee", assignee);

        JsonObject fields = new JsonObject();
        fields.add("fields", field);

        System.out.println(fields.toString());

输出:

{
    "fields": {
        "issuetype": {
            "id": "10004"
        },
        "project": {
            "key": "TES"
        },
        "reporter": {
            "name": "TestUser"
        },
        "summary": {
            "summary": "Screen not responding"
        },
        "description": {
            "description": "New Bug in UI. Screen not responding"
        },
        "assignee": {
            "name": "Test"
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM