繁体   English   中英

如何以角度向后端发送正确的json

[英]how to send a correct json to backend in angular

我正在发送一个数组对象,它现在在我的控制台中看起来像这样:

[{

}]

但我想要这样:

{

}

有人可以告诉我如何将其更改为我想要的形式吗? 这是我的文档数组的样子:

  documentsArray = [];

这就是我的代码的样子:

uploadFile2(files: FileList | null): void {
        const file = files.item(0)
        const reader = new FileReader()
        reader.readAsDataURL(file)
        reader.onload = () => {
          this.documentsArray.push({documentType: this.form.controls.dokumentType.value, file: reader.result})
          console.log(this.documentsArray)
        }
    }

您可以使用索引位置访问数组项:

 const foo = [{ bar: 'baz' }, { quz: 'qux' }]; console.log(foo[0]); // only first item: `{ bar: 'baz' }`

IE:

uploadFile2(files: FileList | null): void {
        const file = files.item(0)
        const reader = new FileReader()
        reader.readAsDataURL(file)
        reader.onload = () => {
          this.documentsArray.push({documentType: this.form.controls.dokumentType.value, file: reader.result});
          // you can use the first object in the array
          console.log(this.documentsArray[0]);
        }
    }

JSON 规范是这样的:

[{ array1 }, {array2}, {other arrays}]

这就是标准的whay,你为什么要跳出标准的方法?? 请注意,以后如果你想用其他平台交换数据,你将重建everithing,因为json数组是[],如果你没有它,json解码将失败,如果你不知道如何处理[ ] 只是临时删除写入/读取字符串中的第一个和最后一个字符,但不是那么好..

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM