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

  • SEARCH
  • Home
  • 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 8907271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:52:22+00:00 2026-06-15T02:52:22+00:00

Using GSON how do I append the class name of my List to my

  • 0

Using GSON how do I append the class name of my List to my outputted json string? I’ve looked through the api and have missed any reference to do this. I’m using GsonBuilder in my real code but don’t see any options for it either.

public class Person {
  String name;

  public Person(String name){
    this.name = name;
  }

  public static void main(String [] args){
    Person one = new Person("Alice");
    Person two = new Person("Bob");

    List<Person> people = new ArrayList<Person>();
    people.add(one);
    people.add(two); 

    String json = new Gson(people);
  }
}

This gives the following output:

json = [{“name”: “Alice”},{“name”: “Bob”}]

How do I achieve the following output? or something similar.

json = {“person”:[{“name”: “Alice”},{“name”: “Bob”}]}

or

json = [{“person”:{“name”: “Alice”}},{“person”:{“name”: “Bob”}}]

Hope it’s something trivial that I have just missed. Thanks in advance.

  • 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-15T02:52:24+00:00Added an answer on June 15, 2026 at 2:52 am

    I don’t know if the answer is still interesting you but what you can do is the following:

    public static void main(String [] args){
        Person one = new Person("Alice");
        Person two = new Person("Bob");
    
        List<Person> people = new ArrayList<Person>();
        people.add(one);
        people.add(two); 
    
        Gson gson = new Gson();
        JsonElement je = gson.toJsonTree(people);
        JsonObject jo = new JsonObject();
        jo.add("person", je);
        System.out.println(jo.toString()); //prints {"person":[{"name": "Alice"},{"name": "Bob"}]}
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Gson to convert a class containing two GeoPoints to a JSON string.
I am using Gson class to convert JSON string data to Intent object. But
I want to parse this JSON string using Gson {name:name,type:[a,b,c]} is it possible? 1st
I have been using GSON library to parse all the json string and get
I am trying to serialize/deserialize JSON in Android using GSON. I have two classes
im trying to convert a json object to a string by using gson here
I have some JSON that I am deserializing using Gson. { resp: { posts:
I have a method for reading JSON from a service, I'm using Gson to
I'm using Gson API and created a class like: public class Person { private
I have some difficulties with json deserialization using GSon and I hope somebody can

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.