Java GSON 无法解析到对象数组
Java GSON Failed parsing to object array
我有问题。我正在做一个网络电话,我在屏幕上打印了一个 json,如下所示:
{"Agents":[{"Id":"1","Owner":"Andre"},{"Id":"7","Owner":"Andre2"},{"Id":"8","Owner":"Andre"},{"Id":"9","Owner":"Alexander"},{"Id":"10","Owner":"Alexander"},{"Id":"12","Owner":"Andre"}]}
然后我用下面的代码从网上获取了json并解析成一个数组对象:
EfyWebAPI webAPI = new EfyWebAPI();
String jsonResponse = webAPI.executeQuery("www.mysite.org/test.php", "SELECT Id, Owner FROM Agents");
Gson gson = new Gson();
Agent[] agents = gson.fromJson(jsonResponse, Agent[].class);
System.out.println(agents[0].getId());
class 看起来像这样:
public class Agent {
private int id;
private String owner;
public Agent(int id, String owner) {
this.id = id;
this.owner = owner;
}
public int getId() {
return this.id;
}
}
但是当我 运行 代码时,我得到以下错误:
Exception in thread "main" com.google.gson.JsonSyntaxException:
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING
at line 1 column 1 path $ at
com.google.gson.Gson.fromJson(Gson.java:822) at
com.google.gson.Gson.fromJson(Gson.java:775) at
com.google.gson.Gson.fromJson(Gson.java:724) at
com.google.gson.Gson.fromJson(Gson.java:696) at
com.company.Main.main(Main.java:18) Caused by:
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING
at line 1 column 1 path $ at
com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350) at
com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:70)
at com.google.gson.Gson.fromJson(Gson.java:810) ... 4 more
为什么会发生这种情况,我该如何解决?
It works kind of... Instead of getting id=1, it gets id=0. I think it gets the id of the object count from the json. How does this work and why can't I parse multiple objects at once and return it into an array list or something?
JSON(或至少 GSON)总是期望一个字符串作为键(据我所知)。只要符合JSON标准JSON就可以,String后面是什么并不重要。
如果您声明类似
,您提到的内容是可能的
{"persons" : [
+ 其余内容 + ]}
,但我不确定您是否可以自动将其解析为 List<Agent>
.
错误是说
Expected BEGIN_ARRAY...
因为第二个参数 (Agent[].class
),它需要 Agent
-s 的有效 json 数组字符串。
您的 json 字符串是一个具有键 Agents
的对象,值是 Agent
的数组。
例如,可能的解决方案是创建一个 class 名为 Agents
,表示 json 对象。
public class Agents {
private Agent[] Agents; // note the capital 'A' matches the key name in the json string
public Agent[] getAgents() {
return Agents;
}
public void setAgents(Agent[] agents) {
this.Agents = agents;
}
}
并调整代理class如下:
public class Agent {
@SerializedName("Id") // note the annotated field is needed since in the json string id is Id
private int id;
@SerializedName("Owner") // same as id field, annotation needed, or rename owner to Owner
private String owner;
public Agent(int id, String owner) {
this.id = id;
this.owner = owner;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public void setOwner(String owner) {
this.owner = owner;
}
@Override
public String toString() {
return "Agent{" +
"id=" + id +
", owner='" + owner + '\'' +
'}';
}
}
这是一个有效的演示:
public class GsonDemo {
public static void main(String[] args) {
String json = "{\"Agents\":[{\"Id\":\"1\",\"Owner\":\"Andre\"},{\"Id\":\"7\",\"Owner\":\"Andre2\"},{\"Id\":\"8\",\"Owner\":\"Andre\"},{\"Id\":\"9\",\"Owner\":\"Alexander\"},{\"Id\":\"10\",\"Owner\":\"Alexander\"},{\"Id\":\"12\",\"Owner\":\"Andre\"}]}";
Gson gson = new Gson();
Agents a = gson.fromJson(json, Agents.class);
System.out.println(a.getAgents()[1]);
System.out.println(a.getAgents().length);
// Output:
// Agent{id=7, owner='Andre2'}
// 6
}
}
我有问题。我正在做一个网络电话,我在屏幕上打印了一个 json,如下所示:
{"Agents":[{"Id":"1","Owner":"Andre"},{"Id":"7","Owner":"Andre2"},{"Id":"8","Owner":"Andre"},{"Id":"9","Owner":"Alexander"},{"Id":"10","Owner":"Alexander"},{"Id":"12","Owner":"Andre"}]}
然后我用下面的代码从网上获取了json并解析成一个数组对象:
EfyWebAPI webAPI = new EfyWebAPI();
String jsonResponse = webAPI.executeQuery("www.mysite.org/test.php", "SELECT Id, Owner FROM Agents");
Gson gson = new Gson();
Agent[] agents = gson.fromJson(jsonResponse, Agent[].class);
System.out.println(agents[0].getId());
class 看起来像这样:
public class Agent {
private int id;
private String owner;
public Agent(int id, String owner) {
this.id = id;
this.owner = owner;
}
public int getId() {
return this.id;
}
}
但是当我 运行 代码时,我得到以下错误:
Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $ at com.google.gson.Gson.fromJson(Gson.java:822) at com.google.gson.Gson.fromJson(Gson.java:775) at com.google.gson.Gson.fromJson(Gson.java:724) at com.google.gson.Gson.fromJson(Gson.java:696) at com.company.Main.main(Main.java:18) Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $ at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350) at com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:70) at com.google.gson.Gson.fromJson(Gson.java:810) ... 4 more
为什么会发生这种情况,我该如何解决?
It works kind of... Instead of getting id=1, it gets id=0. I think it gets the id of the object count from the json. How does this work and why can't I parse multiple objects at once and return it into an array list or something?
JSON(或至少 GSON)总是期望一个字符串作为键(据我所知)。只要符合JSON标准JSON就可以,String后面是什么并不重要。
如果您声明类似
,您提到的内容是可能的{"persons" : [
+ 其余内容 + ]}
,但我不确定您是否可以自动将其解析为 List<Agent>
.
错误是说
Expected BEGIN_ARRAY...
因为第二个参数 (Agent[].class
),它需要 Agent
-s 的有效 json 数组字符串。
您的 json 字符串是一个具有键 Agents
的对象,值是 Agent
的数组。
例如,可能的解决方案是创建一个 class 名为 Agents
,表示 json 对象。
public class Agents {
private Agent[] Agents; // note the capital 'A' matches the key name in the json string
public Agent[] getAgents() {
return Agents;
}
public void setAgents(Agent[] agents) {
this.Agents = agents;
}
}
并调整代理class如下:
public class Agent {
@SerializedName("Id") // note the annotated field is needed since in the json string id is Id
private int id;
@SerializedName("Owner") // same as id field, annotation needed, or rename owner to Owner
private String owner;
public Agent(int id, String owner) {
this.id = id;
this.owner = owner;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public void setOwner(String owner) {
this.owner = owner;
}
@Override
public String toString() {
return "Agent{" +
"id=" + id +
", owner='" + owner + '\'' +
'}';
}
}
这是一个有效的演示:
public class GsonDemo {
public static void main(String[] args) {
String json = "{\"Agents\":[{\"Id\":\"1\",\"Owner\":\"Andre\"},{\"Id\":\"7\",\"Owner\":\"Andre2\"},{\"Id\":\"8\",\"Owner\":\"Andre\"},{\"Id\":\"9\",\"Owner\":\"Alexander\"},{\"Id\":\"10\",\"Owner\":\"Alexander\"},{\"Id\":\"12\",\"Owner\":\"Andre\"}]}";
Gson gson = new Gson();
Agents a = gson.fromJson(json, Agents.class);
System.out.println(a.getAgents()[1]);
System.out.println(a.getAgents().length);
// Output:
// Agent{id=7, owner='Andre2'}
// 6
}
}