0

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?

4
  • Could you give the complete String "keyString"? Commented Jul 14, 2013 at 18:34
  • Basically it could be any string given by user, but for the sake of the discussion, I got this error when keyString was "abc" Commented Jul 14, 2013 at 18:35
  • 2
    "abc"?? That's not valid JSON. Commented Jul 14, 2013 at 18:37
  • Ok...so can you please explain what a valid json is? Commented Jul 14, 2013 at 18:38

2 Answers 2

2

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

{
  "name" : "john"
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.