我正在尝试使用GSON将JSON对象转换为POJO。
JSON字符串
[
{
"automation_project": {
"user_id": null,
"name": "Untitled Project",
"updated_at": "2015-06-16T19:39:42Z",
"group_id": 764496,
"created_at": "2014-11-23T01:01:59Z",
"id": 16214
}
},
{
"automation_project": {
"user_id": null,
"name": "newintropage",
"updated_at": "2015-06-16T21:20:47Z",
"group_id": 764496,
"created_at": "2015-06-16T20:39:04Z",
"id": 29501
}
}
]
与GSON一起使用的AutomationProjectsList类
public class AutomationProjectsList {
private List<AutomationProject> automationProject = new ArrayList<AutomationProject>();
public List<AutomationProject> getAutomationProject() {
return automationProject;
}
public void setAutomationProject(List<AutomationProject> automationProject) {
this.automationProject = automationProject;
}
@Override
public String toString() {
return "AutomationProjectsList [automationProject=" + automationProject
+ "]";
}}
自动化项目POJO
public class AutomationProject {
private Object userId;
private Integer groupId;
private Integer id;
private String name;
private String updatedAt;
private String createdAt;
public Object getUserId() {
return userId;
}
public void setUserId(Object userId) {
this.userId = userId;
}
public Integer getGroupId() {
return groupId;
}
public void setGroupId(Integer groupId) {
this.groupId = groupId;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(String updatedAt) {
this.updatedAt = updatedAt;
}
public String getCreatedAt() {
return createdAt;
}
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}}
我正在使用的代码
JSONArray jsonArray = new JSONArray(response.getEntity(String.class));
for(int i = 0; i < jsonArray.length(); i++){
if(jsonArray.get(i) instanceof JSONObject){
JSONObject jsnObj = (JSONObject)jsonArray.get(i);
AutomationProjectsList obj = new Gson().fromJson(jsnObj.toString(), AutomationProjectsList.class);
System.out.println(obj.getAutomationProject().get(0).getId());
}
}
但它有一个例外:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at br.usp.icmc.teste.ConnectionRestClient.getBrowserStackProjects(ConnectionRestClient.java:74)
at br.usp.icmc.teste.TestePrincipal.main(TestePrincipal.java:9)
为什么我收到IndexOutOfBoundsException异常?我哪里错了?
答案 0 :(得分:4)
您的班级或JSON不正确。我建议你的JSON是。 与您的POJO类匹配的JSON将是:
{
"automationProjects":[
{
"user_id": null,
"name": "Untitled Project",
"updated_at": "2015-06-16T19:39:42Z",
"group_id": 764496,
"created_at": "2014-11-23T01:01:59Z",
"id": 16214
},
{
"user_id": null,
"name": "newintropage",
"updated_at": "2015-06-16T21:20:47Z",
"group_id": 764496,
"created_at": "2015-06-16T20:39:04Z",
"id": 29501
}
]
}
注意我使用了名称automationProjects作为列表,因为它更有意义,所以你的类将是:
public class AutomationProjectsList {
private List<AutomationProject> automationProjects = new ArrayList<AutomationProject>();
public List<AutomationProject> getAutomationProjects() {
return automationProjects;
}
public void setAutomationProjects(List<AutomationProject> automationProjects) {
this.automationProjects = automationProjects;
}
@Override
public String toString() {
return "AutomationProjectsList [automationProject=" + automationProject
+ "]";
}
}
最后将JSON转换为AutomationProjectsList对象:
AutomationProjectsList projectsList = new Gson().fromJson(jsonArray.toString(), AutomationProjectsList.class);
然后,如果您想记录每个项目:
for(AutomationProject project : projectsList.automationProjects){
System.out.println(porject.getId());
}
总之,您的代码似乎有以下问题:
您是否有列表列表或只有一个项目列表?如果列表只是一个,那么为什么要迭代jsonArray
,因为它的子对象就是列表本身?
如果您在JSON上正确建模,那么您就不需要迭代JSON来获取对象
您发布的JSON非常奇怪且不方便与Gson一起使用,这是一项要求还是您可以随意编辑它?
希望这有帮助
既然你说你无法改变你得到的JSON,那么它会变得有点复杂,但是一切都取决于JSON格式的类建模。所以让我们从这个JSON开始:
[
{
"automation_project": {
"user_id": null,
"name": "Untitled Project",
"updated_at": "2015-06-16T19:39:42Z",
"group_id": 764496,
"created_at": "2014-11-23T01:01:59Z",
"id": 16214
}
},
{
"automation_project": {
"user_id": null,
"name": "newintropage",
"updated_at": "2015-06-16T21:20:47Z",
"group_id": 764496,
"created_at": "2015-06-16T20:39:04Z",
"id": 29501
}
}
]
现在,这是非常讨厌的,但让我们看看我们在这里有什么:我们有一个未命名的对象数组,其中包含一个属性"automationProject"
,它是我们实际的AutomationProject对象。因此,在结构方面,它是一个包装实际AutomationProject的对象列表。
因此,您需要摆脱AutomationProjectList并使用更有意义的AutomationProjectWrapper进行更改,并将其视为休耕:
public class AutomationProjectsWrapper {
private AutomationProject automation_project = new AutomationProject();
public AutomationProject getAutomationProject() {
return automation_project;
}
public void setAutomationProject(AutomationProject automationProject) {
this.automation_project = automationProject;
}
@Override
public String toString() {
return "AutomationProjectsList [automationProject=" + automation_project
+ "]";
}
}
请参阅此类等效于JSON对象:
{
"automation_project": {
"user_id": null,
"name": "Untitled Project",
"updated_at": "2015-06-16T19:39:42Z",
"group_id": 764496,
"created_at": "2014-11-23T01:01:59Z",
"id": 16214
}
}
最后,你有一个像jsonArray这样的包装器对象的数组,所以你可以写:
AutomationProjectWrapper[] projectsList = new Gson().fromJson(jsonArray.toString(), AutomationProjectWrapper[].class);
然后记录您的对象:
for(AutomationProjectWrapper wrapper : projectsList){
System.out.println(wrapper.getAutomationProject().getId());
}
很抱歉,在AutomationProjectWrapper
课程中,AutomationProject
字段的名称应为automation_project
。
已在上面的代码中修复。
答案 1 :(得分:1)
根据您的JSON字符串,您尝试访问的值是:
"John-Raj"
在您的代码中,您有:jsonString[i].automation_project.user_id
我认为应该是:obj.getAutomationProject().get(0).getId()