我生成一个包含一系列嵌套对象的对象数组(在元素' failReasons'和'照片') - 然后我想将结果数组发布为JSON到Web API,它会期望一个对象列表,带有嵌套的对象列表。第一部分工作 - 数组接收对象列表并可以解析它们,但是,当将我的数组转换为JSON时,嵌套数组将保留为空。
{"capeNo":"ABCD1234","insDate":"2016-11-10 15:03:18","inspector":"User.Name","inspectionResult":"Pass","comments":"","site":"Site Name","dismCompleteDate":"","failReasons":[],"photos":[]}
正如您所看到的,failReasons在字符串化之前在其数组中有6个对象,现在只是一个空数组。
我使用以下代码生成数组:
接口:
export interface inspectionUpload {
capeNo: string;
insDate: string;
inspector: string;
inspectionResult: string;
comments: string;
site: string;
dismCompleteDate;
failReasons: Array<Object>;
photos: Array<Object>;
}
export interface inspectionFailures {
failCategory: string;
failReason: string;
resolution: string;
inspectionId: string;
}
export interface inspectionPhotos {
base64: string;
}
这会填充数组:
this.sql.query('SELECT rowid,* FROM InspectionResults_tbl').then((res) => {
var arr = []
for (var i = 0; i < res.res.rows.length; i++) {
var item = res.res.rows.item(i);
var header: inspectionUpload = {
capeNo: item.capeNo,
insDate: item.inspectionDT,
inspector: item.inspectedBy,
inspectionResult: item.result,
comments: item.comment,
site: this.SharedService.site,
dismCompleteDate: item.dismantleDate,
failReasons: < inspectionFailures[] > [],
photos: < inspectionPhotos[] > []
}
if (item.result == 'Fail') {
console.log(item);
this.sql.query('SELECT * FROM InspectionFailures_tbl WHERE inspectionId = ?', [item.rowid]).then((res) => {
for (var i = 0; i < res.res.rows.length; i++) {
var item = res.res.rows.item(i);
header.failReasons.push({
failCategory: item.failCategory,
failReason: item.failreason,
resolution: item.resolution,
FRId: item.FRId
})
}
})
}
this.sql.query('SELECT * FROM InspectionPhotos_tbl WHERE inspectionId = ?', [item.rowid]).then((res) => {
for (var i = 0; i < res.res.rows.length; i++) {
var item = res.res.rows.item(i);
header.photos.push({
base64: item.base64
})
}
})
arr.push(header);
if ((i + 1) == res.res.rows.length) upload(arr)
}
})
最后,将其字符串化:
function upload(a) {
console.log(a);
console.log(JSON.stringify(a));
}
非常感谢帮助:)
答案 0 :(得分:0)
这可能是因为您在解析promise之前打印了字符串化数组。
this.sql.query('SELECT * FROM InspectionFailures_tbl WHERE inspectionId = ?', [item.rowid]).then((res) => {
for (var i = 0; i < res.res.rows.length; i++) {
var item = res.res.rows.item(i);
header.failReasons.push({
failCategory: item.failCategory,
failReason: item.failreason,
resolution: item.resolution,
FRId: item.FRId
})
}
})
...
arr.push(header);
if ((i + 1) == res.res.rows.length) upload(arr)
如果你执行下面的代码,它会正确地字符串化(这不是正确的方法,我只是证明你不能进行字符串化,直到当时的推送完成填充数组... ):
var queryComplete = false;
this.sql.query('SELECT * FROM InspectionFailures_tbl WHERE inspectionId = ?', [item.rowid]).then((res) => {
for (var i = 0; i < res.res.rows.length; i++) {
var item = res.res.rows.item(i);
header.failReasons.push({
failCategory: item.failCategory,
failReason: item.failreason,
resolution: item.resolution,
FRId: item.FRId
})
}
queryComplete = true;
})
...
while ( !queryComplete ){
//noop
}
arr.push(header);
if ((i + 1) == res.res.rows.length) upload(arr)