2

I'm trying to figure out how to use gson to convert reddit's api response to a form I can use. After some code I use

System.out.println(response.toString()); 

to get output (slightly edited)

{"json": {"errors": [], "data": {"modhash": "dosiom5o6abbbb758729039f04762a05778db4aeeeacd8eb4a", "cookie": "14756159,2012-08-21T12:05:05,0971bdec35d71af4073cf56ad82fb0ae7c5fe2d1"}}} 

After googling around I built the following class

class GSONClass { private Response jsonresponse; public Response getJsonresponse() { return jsonresponse; } public void setJsonresponse(Response jsonresponse) { this.jsonresponse = jsonresponse; } public static class Response { private String[] errors; private Data data; public Data getData() { return data; } public void setData(Data data) { this.data = data; } public String[] getErrors() { return errors; } public void setErrors(String[] errors) { this.errors = errors; } } public static class Data { private String modhash = "hi"; private String cookie; public String getCookie() { return cookie; } public void setCookie(String cookie) { this.cookie = cookie; } public String getModhash() { return modhash; } public void setModhash(String modhash) { this.modhash = modhash; } } } 

Then I use:

GSONClass target = new GSONClass(); String json = gson.toJson(response.toString()); GSONClass target = gson.fromJson(json, GSONClass.class); 

I'm doing something wrong because I get a "java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING" error.

3
  • The data in the response.toString() does not represent a GSONClass object it represents a a Response object. The google gson library can be used to convert a JSON string to an equivalent Java object not to a completely different java object. Commented Aug 22, 2012 at 1:11
  • response is already json, no? What happens if you pass response directly to gson.fromJson? Commented Aug 22, 2012 at 1:11
  • @antony.trupe response is a stringbuilder. If I try GSONClass target = gson.fromJson(response, GSONClass.class); then I get errors. If I do as written in original post than I appear to get an empty object. Commented Aug 22, 2012 at 1:21

1 Answer 1

1

You were close. Your object needed to have an attribute named json that contained both an array(errors) and an object named data that contained properties modhash and cookie. The property you were calling jsonResponse needed to be called json.

public class GSONClass { private Response json; public static class Response { private String[] errors; private Data data; } public static class Data { private String modhash = "hi"; private String cookie; } } 

And a stub/runner.

import com.google.gson.Gson; public class Main { public static void main(String[] args) { String response = "{\"json\": {\"errors\": [], \"data\": {\"modhash\": \"dosiom5o6abbbb758729039f04762a05778db4aeeeacd8eb4a\", \"cookie\": \"14756159,2012-08-21T12:05:05,0971bdec35d71af4073cf56ad82fb0ae7c5fe2d1\"}}}"; GSONClass target = new Gson().fromJson(response, GSONClass.class); System.out.println(target); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

the getters and setters are not needed to make this work, so I removed them for clarity and succinctness.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.