16

i am fairly new to GSON and get a JSON response of this format (just an easier example, so the values make no sense):

{
    "Thomas": {
        "age": 32,
        "surname": "Scott"
    },
    "Andy": {
        "age": 25,
        "surname": "Miller"
    }
}

I want GSON to make it a Map, PersonData is obviously an Object. The name string is the identifier for the PersonData.

As I said I am very new to GSON and only tried something like:

Gson gson = new Gson();
Map<String, PersonData> decoded = gson.fromJson(jsonString, new TypeToken<Map<String, PersonData>>(){}.getType());

but this threw the error:

Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 3141

Any help is appreciated :)

dev2d
  • 4,245
  • 3
  • 31
  • 54
luuksen
  • 1,357
  • 2
  • 12
  • 38
  • 1
    Your Json is invalid. Need a comma before "Andy" – Taylor Dec 11 '13 at 15:51
  • 1
    I'm sorry, the JSON is definitely valid. I just shortened the JSON and forgot it. It's more about the GSON part on how to interpret it to key-value where value is an object to be serialized by GSON. – luuksen Dec 11 '13 at 15:53

2 Answers2

29

The following works for me

static class PersonData {
    int age;
    String surname;
    public String toString() {
        return "[age = " + age + ", surname = " + surname + "]";
    }
}

public static void main(String[] args) {
    String json = "{\"Thomas\": {\"age\": 32,\"surname\": \"Scott\"},\"Andy\": {\"age\": 25,\"surname\": \"Miller\"}}";
    System.out.println(json);
    Gson gson = new Gson();
    Map<String, PersonData> decoded = gson.fromJson(json, new TypeToken<Map<String, PersonData>>(){}.getType());
    System.out.println(decoded);
}

and prints

{"Thomas": {"age": 32,"surname": "Scott"},"Andy": {"age": 25,"surname": "Miller"}}
{Thomas=[age = 32, surname = Scott], Andy=[age = 25, surname = Miller]}

So maybe your PersonData class is very different.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Okay, I had another problem and I'M sorry that I bothered your time. I had a mistake in my class types that I didn't even post here, but your confirmation that the GSON part works helped me realize where to look at. Thanks! – luuksen Dec 11 '13 at 16:07
  • @luuksen You're welcome. `Gson` is usually pretty good in its error statements. The `BEGIN_ARRAY`, `BEGIN_OBJECT`, `NUMBER` and `STRING` will indicate what the Gson parser is actually reading instead of what is expected. – Sotirios Delimanolis Dec 11 '13 at 16:08
2

You can use gson.toJsonTree(Object o) to convert your custom object to JSON format.

The following works for me:

private static class PersonData {
    private int age;
    private String surname;

    public PersonData(int age, String surname) {
        this.age = age;
        this.surname = surname;
    }
}

public static void main(String[] args) {
    PersonData first = new PersonData(24, "Yovkov");
    PersonData second = new PersonData(25, "Vitanov");

    Gson gson = new Gson();

    JsonObject jsonObject = new JsonObject();
    jsonObject.add("kocko", gson.toJsonTree(first));
    jsonObject.add("deyan", gson.toJsonTree(second));

    System.out.println(gson.toJson(jsonObject));

}

and prints:

{"kocko":{"age":24,"surname":"Yovkov"},"deyan":{"age":25,"surname":"Vitanov"}}
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147