javascript - 如何将类型化数组的打字稿对象解析为xml,包括数组对象名称
问题描述
我正在尝试将Person
Typescript 对象转换为 XML,但转换中不包含“CAR”标签,我该如何实现?
已经尝试使用库 js2xmlparser 和 xml-js,使用 Angular 8 并尝试使用 JSON.Stringify() 但在 JSON 中出现相同的结果,Typescript 数组被编码而没有嵌套在其中的对象的名称。
export class Person {
...
CarsList: Car[];
constructor() {
this.CarsList = [];
}
}
export class Car {
...
Parts: Parts[];
Model: string;
...
}
我实际用于编码为 xml 的代码如下:
import * as xmlJs from 'xml-js';
// This is using the xml-js library to generate the xml in the return
getWithXmlJsLibrary(obj: Person) {
const options = { compact: true, ignoreComment: true, spaces: 4 };
return xmlJs.js2xml(obj, options); // js2xml
}
预期结果是
<Person>
<CarsList>
<Car>
<Model>value</Model>
</Car>
<Car>
<Model>value</Model>
</Car>
<Car>
<Model>value</Model>
</Car>
</Carslist>
</Person>
但实际结果是
<Person>
<CarsList>
<Model>value</Model>
<Model>value</Model>
...
</Carslist>
</Person>
解决方案
推荐阅读
- java - 如何在 Jersey/Dropwizard 中获取变量列表 JSON 正文?
- mysql - 在mysql中创建过程时出现语法错误
- c - 通过自定义排序/删除链表中的重复项跳过最后一项
- python - 在 Python 中重塑数据集
- javascript - Slick Slider - 将图像加载到幻灯片中的 API
- python - 如果一个元组是另一个元组的子集,如何有效地查找
- vb.net - 使用 VB.net 中的 Google Cloud 服务向 Google Drive 上传/下载文件
- ios - 如何在应用商店的产品页面中编辑语言信息?
- c# - wpf计算控件的位置
- javascript - 在 WebdriverIO 上执行 TestRunner 失败