Root name of JSON data format
See original GitHub issueThis is Person.java
@JsonRootName("Person")
public class Person {
private String name;
private double height;
private int age;
@JsonSerialize(using=CustomDateSerializer.class)
private Date date;
@JsonProperty("Address")
private Address address;
//normal constructor
//getters/setters
}
This is my main class:
public class AppJson {
public static void main(String[] arg) throws JsonGenerationException,
JsonMappingException, IOException {
Address a = new Address("Jln Koli", "90121", "Vila", "Belgium");
Person p = new Person("Ali Bin Baba", new Date(), 90.0, 12, a);
Json json = new Json(p);
System.out.println(json.toString());
}
}
Output:
{
"name" : "Ali Bin Baba",
"age" : 12,
"date" : "06-12-2014",
"Address" : {
"postcode" : "90121",
"city" : "Vila",
"state" : "Belgium",
"street" : "Jln Koli"
}
}
But, the output I want is like this:
{
"Person": {
"name": "Ali Bin Baba",
"age": "12",
"date": "06-12-2014",
"Address": {
"postcode": "90121",
"city": "Vila",
"state": "Belgium",
"street": "Jln Koli"
}
}
}
My dependencies:
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.3.3</version>
</dependency>
</dependencies>
Did you know, which part is wrong??
Issue Analytics
- State:
- Created 9 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
JSON root element - Stack Overflow
The outermost level of a JSON document is either an "object" (curly braces) or an "array" (square brackets). Any software that converts JSON ......
Read more >An Introduction to JSON | DigitalOcean
JSON — short for JavaScript Object Notation — is a format for sharing data. As its name suggests, JSON is derived from the...
Read more >Jackson JSON - Using @JsonRootName to customize POJO ...
@JsonRootName annotation is used to indicate name of the POJO that should be serialized. For serialization to work with @JsonRootName ...
Read more >Add a Root Node to JSON Output with the ROOT Option - SQL ...
Here's another example of a FOR JSON clause with the ROOT option. This example specifies a value for the optional RootName argument.
Read more >10 Using JSON Documents
6 Mapping Root-Level Collections. If you use the @XmlRootElement(name="root") annotation to specify a root level, the JSON document can be marshaled ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Two ways to go; either use something like:
to specify root name for your
List
subtype.Or use method in
ObjectWriter
to use whatever name you want for object:You are not using “withRootName(“Person”)” in there.
And from now on, let’s use mailing lists for questions, discussions. Issue trackers are for bugs.