javajsonparsinggson

"Expected BEGIN_OBJECT but was STRING at line 1 column 1"


I have this method:

public static Object parseStringToObject(String json) {
    String Object = json;
    Gson gson = new Gson();
    Object objects = gson.fromJson(object, Object.class);
    parseConfigFromObjectToString(object);
    return objects;
}

And I want to parse a JSON with:

public static void addObject(String IP, Object addObject) {
    try {
        String json = sendPostRequest("http://" + IP + ":3000/config/add_Object", ConfigJSONParser.parseConfigFromObjectToString(addObject));
        addObject = ConfigJSONParser.parseStringToObject(json);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

But I get an error message:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1


Solution

  • Even without seeing your JSON string you can tell from the error message that it is not the correct structure to be parsed into an instance of your class.

    Gson is expecting your JSON string to begin with an object opening brace. e.g.

    {
    

    But the string you have passed to it starts with an open quotes

    "