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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:58:49+00:00 2026-05-26T19:58:49+00:00

I want to deserialize a JSON object (using GSON, because I already use it

  • 0

I want to deserialize a JSON object (using GSON, because I already use it for searializing objects) to a general Map of type Map<String, Object>. It should create Objects of types that do correspond to the according JSON types, i.e. this JSON Object

{ 
    "docID" : "a12345",
    "relation" : ["1", "2", "3"],
    "title" : { "de" : "German Title",
                "en" : "English Title"}
}

should be deserialized in a Map<String, Object> with entries of the following types:
(String, String), (String, List<String>), (String, Map<String, String>).

I tried to use the following code:

class Dictionary_ extends HashMap<String, Object> {
    // nothing to do
};

private static final GsonBuilder GSON_BUILDER = new GsonBuilder();

public Map<String, Object> deserializeJsonString(String jsonString) {
    Dictionary_ d = new Dictionary();
    return GSON_BUILDER.create().fromJson(jsonString, d.getClass());
}

But then the resulting types of created Objects are as follows:
(String, Object), (String, Object), (String, Object)
and I am not able to cast these objects into the corresponding “real” types, because I get class cast exceptions.

I am not able to use POJOs, since the data I am receiving does not have predefined structure in terms of class members.

So, how am I able to deserialize such generic data in according generic Java types?

Update:
I just tried to implement it using JSON Simple, and it works out of the box very easily by the following code:

        Object ret = JSONValue.parse(jsonString);
        return (List<Map<String, Object>>) ret;

Why does it not work that easy with GSON???

  • 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-05-26T19:58:50+00:00Added an answer on May 26, 2026 at 7:58 pm

    The structure that JSON.simple deserialized into was a Map, where each entry key is a String, and each entry value is either a simple data type or another Map.

    Deserializing to such a structure with Gson is unfortunately not as simple as I think it should be, taking a few dozen lines of code. I posted an example of doing so at http://programmerbruce.blogspot.com/2011/06/gson-v-jackson.html. (Search for the phrase “Gson Code to turn any JSON object into a Map” to jump straight to the relevant example.)

    That blog post also includes an example of using Jackson to accomplish the same task with just one line of code. (I highly recommend abandoning Gson in favor of Jackson, whenever possible.)

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

Sidebar

Related Questions

I'm currently convering my ASP.NET v2 application to serialize/deserialize it's objects because I want
want to know why String behaves like value type while using ==. String s1
I'm trying to use JSON.Net to deserialize a JSON object into a C# object.
I am using xstream to de/serialize objects to json. I want to serialize a
I've got a JSON string that I want to convert to a Map structure
I want to deserialize a JSON string which does not necessarily contain data for
I want to deserialize an object but don't know the class up front. So,
I'm having trouble getting Jackson to correctly deserialize json into an object when calling
I've read lots of tutorials on how to deserialize a JSON object to an
I have been happy serializing with javascript objects into JSON using JSON.stringify And sending

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.