Java 应为 BEGIN_ARRAY,但在第 1 行第 2 列处为 BEGIN_OBJECT
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20991386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2
提问by Bishan
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
由于以下原因无法解析 JSON:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为 BEGIN_ARRAY,但在第 1 行第 2 列是 BEGIN_OBJECT
Server Url
服务器网址
public static final String SERVER_URL = "https://maps.googleapis.com/maps/api/timezone/json?location=-37.8136,144.9631×tamp=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 ?
我怎么能解决这个问题?
采纳答案by Brian Roach
You state in the comments that the returned JSON is this:
您在评论中声明返回的 JSON 是这样的:
{
"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:
你告诉 Gson 你有一个Post
对象数组:
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.
你没有。JSON 正好代表一个Post
对象,而 Gson 正在告诉您这一点。
Change your code to be:
将您的代码更改为:
Post post = gson.fromJson(reader, Post.class);
回答by Kamlesh Arya
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);