Search code examples
javajsongson

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 2 path $


I am getting a response from a firebase function and dialogflow which hold some information that I need for further implementations. The response is a JSON, which I hold in mResultFromDialogFlow, which is a HashMap (String, Object) type variable.

I've searched other threads like this, but the strange thing was that my problem is at column 2, not 1 and I don't see the problem in the json. Here is the gson part. The error is thrown on the line with Properties!

Gson _gson = new Gson();
String _json = _gson.toJson(mResultFromDialogFlow.get("parameters"));
Properties data = _gson.fromJson(_json, Properties.class);
mTime = data.getProperty("date"); // String type variable
mDateFromUser = data.getProperty("time"); // String type variable

This is the response JSON:

{"date":"2019-07-19T12:00:00+03:00","time":"2019-07-19T14:00:00+03:00"}

Solution

  • That looks like a JSON string converted to JSON. I suspect mResultFromDialogFlow.get("parameters") already returns a JSON string. With toJson(), you convert a JSON string to JSON.

    If you try

    Properties data = _gson.fromJson(mResultFromDialogFlow.get("parameters"), Properties.class);
    

    it'll probably work.