Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gson: JsonSyntaxException: Expected BEGIN_OBJECT but was STRING

I am trying to convert a JSON String into a HashMap. Here is some of my code:

public static void createSimpleAppointment(String json){
try
    {
    Gson gson = new Gson();
    HashMap<String,String> data = new HashMap<String,String>();
    data = gson.fromJson(json, data.getClass());

The exception is being thrown on the last of those lines (where I set data).

The JSON String is

{"body": "body", "startDate": "2014-05-30 11:00:00", "endDate": "2014-05-30 12:00:00", "location": "location", "subject": "subject!"}

The exception I get is

Exception in thread "main" java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58) Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 3 at com.google.gson.Gson.fromJson(Gson.java:815) at com.google.gson.Gson.fromJson(Gson.java:768) at com.google.gson.Gson.fromJson(Gson.java:717) at com.google.gson.Gson.fromJson(Gson.java:689) at ews.calendar.Calendar.createSimpleAppointment(Calendar.java:70) at ews.main.gateway.Main.main(Main.java:34) ... 5 more Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 3 at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:374) at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:183) at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:145) at com.google.gson.Gson.fromJson(Gson.java:803) ... 10 more

like image 361
Ben Sandler Avatar asked May 30 '14 18:05

Ben Sandler


1 Answers

You can convert the JSON string into Java POJO class as well. The name of instance member in POJO class should be exactly same (case-sensitive) as defined in JSON string.

You don't need to convert the date string to date object that is handled by GsonBuilder#setDateFormat() method by default.

Sample code:

class MyJSONObject {
    private String body;
    private Date startDate;
    private Date endDate;
    private String location;
    private String subject;
    // getter & setter
}

// convert json string to MyJSONObject 
MyJSONObject data = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss")
                              .create().fromJson(jsonString, MyJSONObject.class);

// pretty printing of JSON string back from POJO class object
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(data));

output:

{
    "body": "body",
    "startDate": "May 30, 2014 11:00:00 AM",
    "endDate": "May 30, 2014 12:00:00 PM",
    "location": "location",
    "subject": "subject!"
}
like image 179
Braj Avatar answered Sep 29 '22 08:09

Braj