预计BEGIN_ARRAY,但在第1行第2列为BEGIN_OBJECT [英] 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
服务器网址
public static final String SERVER_URL =https://maps.googleapis.com/maps/api/timezone/ ?JSON位置= -37.8136,144.9631&安培;时间戳= 1389162695&安培;传感器=假;
执行请求
try {
//创建一个HTTP客户端
HttpClient客户端= HttpClientBuilder.create()。build();
HttpPost post = new HttpPost(SERVER_URL);
//执行请求并检查状态码
HttpResponse response = client.execute(post);
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode()== 200){
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
尝试{
//读取服务器响应并尝试将其解析为JSON
Reader reader = new InputStreamReader(content);
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setDateFormat(M / d / yy hh:mm a);
Gson gson = gsonBuilder.create();
列表< Post> postsList = Arrays.asList(gson.fromJson(reader,
Post []。class));
content.close();
(Post p:postsList){
System.out.println(p.timeZoneId);
}
} catch(Exception ex){
System.out.println(无法解析JSON,因为:+ ex);
} else {
System.out.println(服务器响应状态码:
+ statusLine.getStatusCode());
} catch(Exception ex){
System.out
.println(无法发送HTTP POST请求,原因是:+ ex);
}
发布课程 $ b
public class Post {
public String timeZoneId;
public Post(){
}
}
<
您在评论中声明返回的JSON是这样的:
{
dstOffset:3600,
rawOffset:36000,
status:OK ,
timeZoneId:澳大利亚/霍巴特,
timeZoneName:澳大利亚东部夏令时
}
你告诉Gson你有一个 Post
对象的数组:
列表< Post> postsList = Arrays.asList(gson.fromJson(reader,
Post []。class));
你没有。 JSON恰好代表一个 Post
对象,而Gson告诉你。
将您的代码更改为:
Post post = gson。来自Json(读者,Post.class);
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×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 ?
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);
这篇关于预计BEGIN_ARRAY,但在第1行第2列为BEGIN_OBJECT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!