Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8920753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:21:28+00:00 2026-06-15T06:21:28+00:00

see following code. I need to serialize a list of data into JSON array:

  • 0

see following code. I need to serialize a list of data into JSON array:

FooEntity.java:

public class FooEntity {

String foo;
String bar;

public String getFoo() {
    return foo;
}

public void setFoo(String foo) {
    this.foo = foo;
}

public String getBar() {
    return bar;
}

public void setBar(String bar) {
    this.bar = bar;
}
}

FooList.java:

public class FooList {
public List<FooEntity> fooList;

public FooList() {
    this.fooList = new ArrayList<FooEntity>();
}

public void add(FooEntity fooEntity) {
    this.fooList.add(fooEntity);
}

public List<FooEntity> getFooList() {
    return fooList;
}

public void setFooList(List<FooEntity> fooList) {
    this.fooList = fooList;
}
}

Here I create the list of FooEntities and serialize it into JSON:

public void fooListToJson() throws IOException {
    FooList fooList = new FooList();

    FooEntity fooEntity1 = new FooEntity();
    fooEntity1.setBar("fooEntity1 bar value");
    fooEntity1.setFoo("fooEntity1 foo value");

    FooEntity fooEntity2 = new FooEntity();
    fooEntity2.setBar("fooEntity2 bar value");
    fooEntity2.setFoo("fooEntity2 foo value");

    fooList.add(fooEntity1);
    fooList.add(fooEntity2);

    ObjectMapper mapper = new ObjectMapper();
    StringWriter stringWriter = new StringWriter();
    final JsonGenerator jsonGenerator = mapper.getJsonFactory().createJsonGenerator(stringWriter);

    mapper.writeValue(jsonGenerator, fooList);

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

So the output is followting:

{"fooList":[{"foo":"fooEntity1 foo value","bar":"fooEntity1 bar value"},{"foo":"fooEntity2 foo value","bar":"fooEntity2 bar value"}]}

That’s all is correct, I have here only one need. I need to change the “root” element of the list. Right now there is “fooList” as root element – I need to change it.

I’ve found few threads and posts to this topic, but nothing was working for me exactly as I want. The solution have to keep the possibility to deserialize the JSON back to corresponding java classes.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:21:29+00:00Added an answer on June 15, 2026 at 6:21 am

    Does @JsonRootName provide what you need?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've a little problem with GSON-JSON. Let see the following code: public static class
See following code with resides in fillowing directory mypack.pack1 package mypack.pack1; public class myclass
See the following code of the <?php class DefaultController extends Controller { public function
Please see the following code to understand my question better. class compareByValue { public:
Please see the following code: List list = Collections.synchronizedList(new ArrayList()); // ... synchronized (list)
Given the following code, i see no more need for the finally block for
I'm trying to figure out how to serialize the following class into XML (in
How can I implement cross browser opacity gradient (not color gradient)? See following code:
How do we update a textbox on ticking checkboxes? Please see the following code,
The following code does not want to compile. See the included error message. Code:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.