2016-03-17 31 views
0

如何使用JSONObject在Java中创建类似以下的JSON对象?如何使用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"); 

任何人可以帮助我解决这个?

感谢

回答

0

尝试以下,

 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" 
     } 
    } 
} 
+1

谢谢@RakeshKR帮助。 – ImGenie

+0

我认为“摘要”:“屏幕没有响应”,与“摘要”不一样:{ “摘要”:“屏幕没有响应” },请检查 – Raghavendra

+0

@Raghavendra change'field.add(“summary”,摘要);'as'field.addProperty(“summary”,“Screen not responding”);'为了支持。 –

1

您应该使用Json工厂类创建对象builders

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(); 
+0

谢谢@barsik让我试试这个。 – ImGenie

相关问题