DanielY
DanielY

Reputation: 1171

Create an object with JSON - Exception received

Given a string keyString, I do in my Java program the following:

ObjectId key = new Gson().fromJson(keyString, ObjectId.class);

But for this line I get this exception:

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

What does that mean, and what I can do to solve this?

Upvotes: 0

Views: 176

Answers (2)

Wand Maker
Wand Maker

Reputation: 18772

It means your JSON does not start with "{". Objects in JSON look something like this:

{
  "name" : "john"
}

Upvotes: 2

Avi
Avi

Reputation: 21866

The fromJson method awaits a json object. This means a string in json format and specifically it needs to start with { (and not [).

This is valid:

{ 'id': '1234'}

This is not valid:

[{ 'id': '1234'}]

This is also not valid:

abc

Upvotes: 1

Related Questions