预期BEGIN_ARRAY但在第1行第2列是BEGIN_OBJECT

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了预期BEGIN_ARRAY但在第1行第2列是BEGIN_OBJECT相关的知识,希望对你有一定的参考价值。

我低于错误。

由于以下原因无法解析JSON:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期BEGIN_ARRAY但在第1行第2列是BEGIN_OBJECT

服务器URL

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

执行请求

    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);
    }

发表课程

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

    }
}

我该怎么解决这个问题?

答案

您在注释中声明返回的JSON是这样的:

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

你告诉Gson你有一组Post对象:

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

你没有。 JSON代表一个Post对象,Gson告诉你。

将您的代码更改为:

Post post = gson.fromJson(reader, Post.class);
另一答案

你得到的回应是对象形式,即

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

替换下面的代码行:

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

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

以上是关于预期BEGIN_ARRAY但在第1行第2列是BEGIN_OBJECT的主要内容,如果未能解决你的问题,请参考以下文章

java.lang.IllegalStateException:应为 BEGIN_ARRAY,但在第 1 行第 2 列是 BEGIN_OBJECT

获得错误预期BEGIN_ARRAY但在第1行第2列路径$是BEGIN_OBJECT

AsyncTask 预期为 BEGIN_OBJECT,但在第 1 行第 2 列路径 $ 处为 BEGIN_ARRAY

预期为 BEGIN_ARRAY,但在第 1 行第 2 列改造时为 BEGIN_OBJECT2

改造预期的 begin_array 但在第 1 行第 2 列路径 $ Android Studio 处是 begin_object

Retrofit2 Android:预期 BEGIN_ARRAY 但在第 1 行第 2 列路径 $ 处为 BEGIN_OBJECT