50

I'm getting below error.

Failed to parse JSON due to: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2

Server Url

public static final String SERVER_URL = "https://maps.googleapis.com/maps/api/timezone/json?location=-37.8136,144.9631&timestamp=1389162695&sensor=false";

Perform the request

    try {
        // Create an HTTP client
        HttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(SERVER_URL);

        // Perform the request and check the status code
        HttpResponse response = client.execute(post);
        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();

            try {
                // Read the server response and attempt to parse it as JSON
                Reader reader = new InputStreamReader(content);

                GsonBuilder gsonBuilder = new GsonBuilder();
                gsonBuilder.setDateFormat("M/d/yy hh:mm a");
                Gson gson = gsonBuilder.create();
                List<Post> postsList = Arrays.asList(gson.fromJson(reader,
                        Post[].class));

                content.close();

                for (Post p : postsList) {
                    System.out.println(p.timeZoneId);
                }

            } catch (Exception ex) {
                System.out.println("Failed to parse JSON due to: " + ex);
            }
        } else {
            System.out.println("Server responded with status code: "
                    + statusLine.getStatusCode());
        }
    } catch (Exception ex) {
        System.out
                .println("Failed to send HTTP POST request due to: " + ex);
    }

Post class

public class Post {
    public String timeZoneId;
    public Post() {

    }
}

How could I solve this ?

3
  • The resource you're invoking, are you sure it returns an array of objects or just a single one?
    – Charlie
    Jan 8, 2014 at 9:22
  • It returns this. { "dstOffset" : 3600, "rawOffset" : 36000, "status" : "OK", "timeZoneId" : "Australia/Hobart", "timeZoneName" : "Australian Eastern Daylight Time" }
    – Bishan
    Jan 8, 2014 at 9:24
  • Because your JSON isn't an array, and it's telling you that. Jan 8, 2014 at 9:25

2 Answers 2

70

You state in the comments that the returned JSON is this:

{ 
  "dstOffset" : 3600, 
  "rawOffset" : 36000, 
  "status" : "OK", 
  "timeZoneId" : "Australia/Hobart", 
  "timeZoneName" : "Australian Eastern Daylight Time" 
}

You're telling Gson that you have an array of Post objects:

List<Post> postsList = Arrays.asList(gson.fromJson(reader,
                    Post[].class));

You don't. The JSON represents exactly one Post object, and Gson is telling you that.

Change your code to be:

Post post = gson.fromJson(reader, Post.class);
0
14

Response you are getting is in object form i.e.

{ 
  "dstOffset" : 3600, 
  "rawOffset" : 36000, 
  "status" : "OK", 
  "timeZoneId" : "Australia/Hobart", 
  "timeZoneName" : "Australian Eastern Daylight Time" 
}

Replace below line of code :

List<Post> postsList = Arrays.asList(gson.fromJson(reader,Post.class))

with

Post post = gson.fromJson(reader, Post.class);
4
  • Yes. I'm using the same way.
    – Bishan
    Jan 8, 2014 at 9:28
  • you are using post[].class and i am suggesting post.class Jan 8, 2014 at 9:29
  • How to use this post?
    – Rohan
    Sep 22, 2017 at 19:27
  • @rohan You need to create a POJO class called Post using JSON response attributes.
    – Bishan
    Jan 5, 2018 at 3:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.