0

How to deserialize the following Json using Google GSON? I tried to deserialize the following JSON:

{
    "version": "1.0",
    "data": {
        "person1": {
            "detailsOffather": {

                "name": "xyz",
                "phone": "8000452234",
                "address1": "abc xyz",
                "address 2": "abcd wxyz",
                "gender": "male"

            },
            "detailsOfmother": {

                "name": "123",
                "phone": "9988776644",
                "address1": "st johns street",
                "address 2": "canada",
                "gender": "female"

            },
            "detailsOfSon": {

                "name": "abc",
                "phone": "8877665544",
                "address1": "church street",
                "address 2": "canada",
                "gender": "male"

            }
        },
        "person2": {
            "detailsOffather": {

                "name": "xyz",
                "phone": "8000452234",
                "address1": "abc xyz",
                "address 2": "abcd wxyz",
                "gender": "male"

            },
            "detailsOfmother": {

                "name": "123",
                "phone": "9988776644",
                "address1": "st johns street",
                "address 2": "canada",
                "gender": "female"

            },
            "detailsOfSon": {

                "name": "abc",
                "phone": "8877665544",
                "address1": "church street",
                "address 2": "canada",
                "gender": "male"

            }
        },
        "person3": {
            "detailsOffather": {

                "name": "xyz",
                "phone": "8000452234",
                "address1": "abc xyz",
                "address 2": "abcd wxyz",
                "gender": "male"

            },
            "detailsOfmother": {

                "name": "123",
                "phone": "9988776644",
                "address1": "st johns street",
                "address 2": "canada",
                "gender": "female"

            },
            "detailsOfSon": {

                "name": "abc",
                "phone": "8877665544",
                "address1": "church street",
                "address 2": "canada",
                "gender": "male"

            }
        }
    }
}

using the following class structure:

class information{
  String version;
  HashMap<String,Details> data;
}

class Details
{
HashMap<String,String> details;
}

Using Google GSON as follows :

Gson gson=new Gson();
gson.fromJson(JsonFile,Information.class)

However, I am not able to deserialize it, can someone help me with it?Note : I need to maintain the data types and the structure of Json.

coder
  • 11
  • 5
  • Why are you asking "how to *deserialize*" if "it doesn't *serialize*"? – Andreas Apr 17 '20 at 05:10
  • **Unable to reproduce.** Perhaps `JsonFile` is not what you think it is. – Andreas Apr 17 '20 at 05:17
  • @ADM No, It didn't. All the previous questions on stack overflow suggest a solution only when the class contains only a map. If it contains a Map with a string or any other data type, I am not able to find a solution to it. – coder Apr 17 '20 at 05:51
  • @Andreas This is the JSON file. Do you have a solution to this? – coder Apr 17 '20 at 05:52
  • Three options: 1) Change `data` to be a `Map> data`, or 2) change `Details` to be `class Details extends HashMap`, or 3) use `@JsonAdapter` on the `Details` class (more complex solution). --- The classes you created expects the JSON to be `{ "version": "1.0", "data": { "detailsOffather": { "details": { "name": "xyz", ... } }, "detailsOfmother": { "details": { "name": "123", ...`, which you'd see if you tried to write JSON from the classes. Writing JSON is actually the best way to debug why reading is not mapping as you expected, so try that next time. – Andreas Apr 17 '20 at 15:14
  • @Andreas As you can see, it isn't necessary that the structure is same! it can be like above! How do I use JsonAdapter with the above structure? – coder Apr 20 '20 at 05:09
  • So you're saying you don't even know the structure of the JSON? Then you parse into `Map`, and check the actual value of a property when you iterate, to see if it's a `List` *(JSON Array)*, `Map` *(JSON Object)*, or `String`, `Number`, `Boolean`, `null` *(JSON Primitive)*. – Andreas Apr 20 '20 at 05:38

1 Answers1

-1

Unable to reproduce. The code runs fine.

Posting as an answer so I can include a full Minimal, Reproducible Example.

The code has gson-2.8.5.jar on the classpath.

import java.util.HashMap;

import com.google.gson.Gson;

public class Test {
    public static void main(String[] args) throws Exception {
        String input = "{\"version\": \"1.0\",\n" + 
                       "    \"details\": {\n" + 
                       "\n" + 
                       "        \"name\": \"xyz\",\n" + 
                       "        \"phone\": \"8000452234\",\n" + 
                       "        \"address1\": \"abc xyz\",\n" + 
                       "        \"address 2\": \"abcd wxyz\",\n" + 
                       "        \"gender\": \"male\"\n" + 
                       "\n" + 
                       "    }\n" + 
                       "}";
        Gson gson=new Gson();
        information info = gson.fromJson(input,information.class);
        System.out.println(info.version);
        System.out.println(info.details);
    }
}
class information{
    String version;
    HashMap<String,String> details;
}

Output

1.0
{gender=male, phone=8000452234, address1=abc xyz, address 2=abcd wxyz, name=xyz}
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Dear Andreas, This only works when the Map is present in the outer class, check the structure I updated just now – coder Apr 17 '20 at 05:54