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 8979511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:51:50+00:00 2026-06-15T19:51:50+00:00

i am attempting to turn a json string into objects with gson. I have

  • 0

i am attempting to turn a json string into objects with gson.

I have a very simple example below, and it runs, but the resulting answer is empty, ie: my Answer objects’s text field is empty.

import com.google.gson.*;

public class Meow {

    public static void main(String[] args) throws Exception{

        Gson gson = new Gson();
        String jsonOutput = "[{\"answer\":{\"text\":\"text1\"}},{\"answer\":{\"text\":\"text2\"}} ]";

        Answer[] a = gson.fromJson(jsonOutput, Answer[].class);

        for(Answer i:a) {
          System.out.println(i.text);
        }       
    }

    public class Answer {

        public String text;

        public Answer(String text) {
            super();
            this.text=text;
        }

        public String toString(){
            return text;
        }

        public void setText(String a){
            this.text=a;
        }
    }

}
  • 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-15T19:51:52+00:00Added an answer on June 15, 2026 at 7:51 pm

    Because your JSON doesn’t match your class.

    Your JSON right now is an array of objects, each containing an answer object as a field.

    Your JSON the way you have things would need to look like:

    String jsonOutput = "[{\"text\":\"text1\"},{\"text\":\"text2\"}]";
    

    Edit to add from comments:

    If you can’t change the output, you need a “wrapper”. Something like:

    public class AnswerWrapper {
        public Answer answer;
    
        // etc
    }
    

    And use an array of those. That is what the JSON will map to. It can’t see them as Answer objects because … they’re not.

    One More Edit to Add: Your other option is to write custom deserializers for your classes. I’m a bit mixed on whether you should do this or not, but it will work. The reason I say that is that you have JSON that isn’t an array of Answer objects, but you want it to be. I think I’d be annoyed if I came across this in production code because without understanding what was going on it could be confusing.

    With that caveat being said, you can create a custom JsonDeserializer and use GsonBuilder:

    class AnswerDeserializer implements JsonDeserializer<Answer> {
    
        public Answer deserialize(JsonElement je, Type type, 
                                  JsonDeserializationContext jdc) 
                                       throws JsonParseException {
    
            return new Answer(je.getAsJsonObject().get("answer")
                                .getAsJsonObject().get("text").getAsString());
        }
    
    }
    

    Then your code would look like:

    public static void main(String[] args) throws Exception{
    
        String jsonOutput = "[{\"answer\":{\"text\":\"text1\"}},{\"answer\":{\"text\":\"text2\"}} ]";
    
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeAdapter(Answer.class, new AnswerDeserializer());
    
        Gson gson = gsonBuilder.create();
    
        Answer[] a = gson.fromJson(jsonOutput, Answer[].class);
    
        for(Answer i:a) {
            System.out.println(i.text);
        }       
    }
    

    If it were me, and I had JSON that wasn’t what I needed it to be but wanted to use GSON to directly serialize/deserialize I’d create the Answer class as a wrapper that hid the details:

    /**
     *  Due to how our JSON is being provided we created an inner
     *  class. 
     **/ 
    public class Answer {
    
        private RealAnswer answer;
    
        private class RealAnswer {
    
            public String text;
        }
    
        ...
    }
    

    With the public getters/setters for Answer accessing the private RealAnswer. It just seems way cleaner and easier to understand for the next guy.

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

Sidebar

Related Questions

I am attempting to take a list of objects, and turn that list into
I have a JSON string I've parsed from a webpage, and I'm attempting to
I've got a module that I'm attempting to turn into a proper OTP application.
I'm attempting to build a very simple wxPython GUI that monitors and displays external
EDIT: Let me turn this into a straight SQL question... I have a varbinary(max)
I'm attempting to wrap a user-inputted string in double quotes for output but I
Possible Duplicate: How to turn a string into a method call? Using Ruby 1.9,
I am attempting to turn an iPhone app into a Universal App with a
I am attempting to create a Python script that in turn runs the shell
I am attempting to turn an array list of strings into an arraylist of

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.