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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:32:31+00:00 2026-06-17T07:32:31+00:00

I have a User object with this structure: class User { private String id;

  • 0

I have a User object with this structure:

class User {
    private String id;
    private String name;
    private Map<String, Object> properties;

    // GETTERS & SETTERS
}

I have a JSON String with this structure:

{
    "user": {
        "id:"123456789",
        "name:"azerty",
        "emailHash":"123456789", // not used in User class
        "properties": {
            "p1":1,
            "p2":"test",
            "p3":[1, 2, 3, 4],
            "p4":{
               etc...
            }
        }
    }
}

Properties’ keys are String, Properties’ values can be a String, int, Array, boolean, Map etc.

I try to deserialize this JSON string with Gson like that:

JsonParser parser = new JsonParser();
JsonElement element = parser.parse(jsonString);
JsonObject object = element.getAsJsonObject();

GsonBuilder builder = new GsonBuilder();
builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
Gson gson = builder.create();
User user = (User) gson.fromJson(object.get("user"), new TypeToken<User>() {}.getType());

Fields ‘id’ and ‘name’ are correctly injected but the field ‘properties’ stays null.

Do you know what I’m doing wrong?
Thanks in advance for your help!

  • 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-17T07:32:32+00:00Added an answer on June 17, 2026 at 7:32 am

    For me this code:

    public class Main {
        public static void main(String[] args) throws IOException {
        GsonBuilder builder = new GsonBuilder();
        builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
        Gson gson = builder.create();
        FileInputStream inputStream = new FileInputStream(new File("bobi.json"));
        InputStreamReader reader = new InputStreamReader(inputStream);
        User user = gson.fromJson(reader, User.class);
        System.out.println(user.getName());
        System.out.println(user.getId());
        for (String property : user.getProperties().keySet()) {
            System.out.println("Key: " + property + " value: " + user.getProperties().get(property));
        }
        reader.close();
    }
    

    Prints this:

    azerty
    123456789
    Key: p1 value: 1.0
    Key: p2 value: test
    Key: p3 value: [1.0, 2.0, 3.0, 4.0]
    Key: p4 value: {}
    

    However, keep in mind that I have stripped the wrapping json object in the file I parse. The file is:

    {
            "id":"123456789",
            "name" : "azerty",
            "emailHash":"123456789", 
            "properties": {
                "p1":1,
                "p2":"test",
                "p3":[1, 2, 3, 4],
                "p4":{
    
                }
            }
    }
    

    Also I added closing double quote for name and id, which you did not have in your sample.

    The User class as requested by the OP. I have added getters and setters for the reason of printing:

    import java.util.Map;
    
    class User {
        private String id;
        private String name;
        private Map<String, Object> properties;
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Map<String, Object> getProperties() {
            return properties;
        }
        public void setProperties(Map<String, Object> properties) {
            this.properties = properties;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following scenario: I have user object: ... @Column(name = TITLE) private String title; @NotNull
Let's say I have an object - a User object in this case -
I have a script which creates a user-defined object like this: obj = new
Currently I have this code var App = Ember.Application.create(); App.user = Ember.Object.create({ people: customers
I have an object that I save like this in my controller: @user =
This is the common structure of all of my classes: public class User {
As a veteran of mongodb, I have created the following structure: User: { name:
Here is the basic structure of my User class: class User { private $_userId
I have a basic structure. A User object and a UserDetails object. The User
I have an object like follows: @connection.register class User(Document): __collection__ = 'users' __database__ =

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.