Reputation: 364
I have tried to parse this json
{"status":"ok","results":{"query":{"id":"6081","name":"UB1656-Palazzo Versace Resort in Culture Village","query":"Aswathy VS : <\/strong>
Test Query","add_date":"2014-08-05 08:27:45"},"replies":[{"replied_by":"Ventures Onsite","replied_on":"05-Aug-2014 09:05:19 AM","reply":"We have received your Test Query."},{"replied_by":"Me","replied_on":"05-Aug-2014 01:28:00 PM","reply":"Thanks... "}]}}
My Pojo classs
import java.util.ArrayList;
public class QueryRepliesList {
public String status;
public ArrayList<Result> results;
public class Replies {
public String id;
public String name;
public String query;
public String add_date;
public String replied_by;
public String replied_on;
public String reply;
}
public class Result {
public ArrayList<Replies> query;
public ArrayList<Replies> replies;
}
}
I trie dto call this by
QueryRepliesList queryReply = gson.fromJson(reader,
QueryRepliesList.class);
But i got the following exception
Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT
I tried by changing
the Pojo class several times as suggested by similar answer to the questions But nothing worked Please some one help Thanks in advance
Upvotes: 2
Views: 5025
Reputation: 3118
Based on the JSON
you have provided, I'm not seeing how you would get the error provided, instead you would see
Expected BEGIN_ARRAY but was BEGIN_OBJECT
since both results
and query
are JSON_OBJECT
. To illustrate, I've put everything into a junit test.
@org.junit.Test
public void asPosted() {
final String json = "{'status':'ok','results':{'query':{'id':'6081','name':'UB1656-Palazzo Versace Resort in Culture Village','query':'Aswathy VS : <\\/strong>\n"
+ "Test Query','add_date':'2014-08-05 08:27:45'},'replies':[{'replied_by':'Ventures Onsite','replied_on':'05-Aug-2014 09:05:19 AM','reply':'We have received your Test Query.'},{'replied_by':'Me','replied_on':'05-Aug-2014 01:28:00 PM','reply':'Thanks... '}]}}";
QueryRepliesContainer data = new Gson().fromJson(json, QueryRepliesContainer.class);
Assert.assertEquals("ok", data.getStatus());
Assert.assertEquals(2, data.getRepliesCount());
Assert.assertEquals("6081", data.getQueryId());
}
Generally, you'll create a POJO for each JSON_OBJECT
. Exceptions for this would include providing a custom deserializer. Depending on the degree to which you need to expose the "inner" data, you'll need to migrate the inner classes out for public level exposure. I've nested them respective to the JSON
structure. So based on what has been provided you'll need something similar to the following:
class QueryRepliesContainer {
private String status;
private Result results;
public String getStatus() {
return status;
}
public int getRepliesCount() {
return results.replies.size();
}
public String getQueryId() {
return results.query.id;
}
private class Result {
private Query query;
private List<Replies> replies;
private class Query {
private String id;
private String name;
private String query;
}
private class Replies {
private String add_date;
private String replied_by;
private String replied_on;
private String reply;
}
}
}
Upvotes: 5