我在与Jackson的Spring Boot 1.5应用程序中将JSON反序列化为Java对象时遇到了一些麻烦。
如下所示,JSON响应是一个由具有一些嵌套属性的单个JSON对象组成的数组:
[
{
"deploymentProject": {
"id": 57966596,
"name": "MyApp 6.3"
},
"environmentStatuses": [{
"environment": {
"id": 57245736,
"name": "Dev1",
"deploymentProjectId": 57966596
},
"deploymentResult": {
"deploymentVersionName": "App-51",
"id": 59769040
}
},
{
"environment": {
"id": 57245737,
"name": "Dev2",
"deploymentProjectId": 57966596
},
"deploymentResult": {
"deploymentVersionName": "App-51",
"id": 59769041
}
}
]
}
]
ResultData.java
我不关心deploymentProject
属性,所以我只在environmentStatuses
中加入ResultData
。
@JsonIgnoreProperties(ignoreUnknown = true)
public class ResultData {
private EnvironmentStatus[] environmentStatuses;
// Getters and setters omitted
}
EnvironmentStatus.java
@JsonIgnoreProperties(ignoreUnknown = true)
public class EnvironmentStatus {
private Environment environment;
private DeploymentResult deployment;
// Getters and setters omitted
}
Environment.java
@JsonIgnoreProperties(ignoreUnknown = true)
public class Environment {
private long id;
private String name;
private String deploymentProjectId;
//Getters and setters omitted
}
DeploymentResult.java
@JsonIgnoreProperties(ignoreUnknown = true)
public class DeploymentResult {
private long id;
private String deploymentVersionName;
// Getters and setters omitted
}
当我在服务类中调用RestTemplate时,environmentStatuses
数组为null
:
Service.java
ResponseEntity<List<ResultData>> response = restTemplate.exchange(uriBuilder.toUriString(),
HttpMethod.GET, null, new ParameterizedTypeReference<List<ResultData>>() {});
return response.getBody();
答案 0 :(得分:1)
public ArrayList<SectionsBean> getAllSectionsByClassesID(){
String selectQuery = "SELECT " + "1 AS" + BaseColumns._ID + "," + COLUMN_CLASSES_NAME +
"," + COLUMN_CLASSLINK + "," + COLUMN_CLASSES_SECTIONS_NAME + "," + COLUMN_SECTIONLINK
+ " FROM " + TABLE_CLASSES_SECTIONS_LINK +
" INNER JOIN " + TABLE_CLASSES + " ON " + TABLE_CLASSES + "." + COLUMN_CLASSES_ID +
" = " + TABLE_CLASSES_SECTIONS_LINK + "." + COLUMN_CLASSLINK +
" INNER JOIN " + TABLE_CLASSES_SECTIONS + " ON " + TABLE_CLASSES_SECTIONS + "." +
COLUMN_CLASSES_SECTIONS_ID + " = " + TABLE_CLASSES_SECTIONS_LINK + "." + COLUMN_SECTIONLINK ;
SQLiteDatabase db = this.getReadableDatabase();
ArrayList<SectionsBean> sectionsBeanList = new ArrayList<SectionsBean>();
Cursor cursor = db.rawQuery(selectQuery,null);
Log.i("Query details", String.valueOf(cursor));
Log.d("DataDetails", DatabaseUtils.dumpCursorToString(cursor));
while (cursor.moveToNext()) {
ClassesBean classesBean = new ClassesBean();
classesBean.setId(cursor.getLong(cursor.getColumnIndex(COLUMN_CLASSES_ID)));
classesBean.setClasses_name(cursor.getString(cursor.getColumnIndex(COLUMN_CLASSES_NAME)));
SectionsBean sectionsBean = new SectionsBean();
sectionsBean.setSectionsID(cursor.getLong(cursor.getColumnIndex(COLUMN_CLASSES_SECTIONS_ID)));
sectionsBean.setSections_name(cursor.getString(cursor.getColumnIndex(COLUMN_CLASSES_SECTIONS_NAME)));
sectionsBean.setClassesBean(classesBean);
sectionsBeanList.add(sectionsBean);
}
return sectionsBeanList;
}
是下一个问题..
如果您希望在代码中使用此名称,则需要使用json结构名称进行批注。或者如果没有将其命名为private DeploymentResult deployment;