user2144555
user2144555

Reputation: 1313

Create an object from JSON (GSON: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING)

I want to read the result of a GET method of REST services, using a Client Application that I build using HttpUrlConnection. That method returns information about an User. After read it, I want to create an object of the User type, with all the information of that User filled. I think I have to convert it into JSON first, right? I'm using GSON.

What I have is:

if(urlConnection.getResponseCode()==200)
{
   String response ="";
   Scanner inStream = new Scanner(urlConnection.getInputStream());

   while(inStream.hasNextLine())
      response+=(inStream.nextLine());
   System.out.println(response);

   //JSON
   Gson gson = new Gson();
   String json = gson.toJson(response);
   System.out.println(json);

   // User Object
   User object = new User();
   object = gson.fromJson(json, User.class);
   System.out.println(object);

}

When I do the first print, I receive:

{"userID":"user2","isMale":false,"isObject":false,"telephone":"+911111111","email":"[email protected]","birthdate":"2012-08-01","firstName":"Maria","lastName":"Silva","isocountrycode":"PT"}

When I do the second print, I receive:

"{\"userID\":\"user2\",\"isMale\":false,\"isObject\":false,\"telephone\":\"+911111111\",\"email\":\"[email protected]\",\"birthdate\":\"2012-08-01\",\"firstName\":\"Maria\",\"lastName\":\"Silva\",\"isocountrycode\":\"PT\"}"

But when I try to print the User object I receive this error:

    com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 226
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
    at com.google.gson.Gson.fromJson(Gson.java:795)
    at com.google.gson.Gson.fromJson(Gson.java:761)
    at com.google.gson.Gson.fromJson(Gson.java:710)
    at com.google.gson.Gson.fromJson(Gson.java:682)
    at httpURLconnection.UserGetUserInfo.main(UserGetUserInfo.java:70)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 226
    at com.google.gson.stream.JsonReader.expect(JsonReader.java:339)
    at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:322)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:165)
    ... 5 more

Can you tell me what am I doing wrong? All the User object fields were filled using that User class.

User class:

    public class User {
    String  userID        = null;
    boolean isMale        = false;
    boolean isObject      = false;
    String  Telephone     = null;
    String  Email         = null;
    Date    Birthdate     = null;
    String  FirstName     = null;
    String  LastName      = null;
    String  ISOcountrycode   = null;

    (...) 
    }

Upvotes: 4

Views: 9458

Answers (1)

Steve Benett
Steve Benett

Reputation: 12933

In your USER class you have to declare all datatypes, not more and not less, which the JSONObject contains. F.e.: class User { String UserID = ""; Boolean isMale;}

and so on.

Otherwise the GSON.toJson(); will not work and throws the exception.

Upvotes: 1

Related Questions