[英]How to generate JSON file from key - value custom class in Typescript
I have issues with finding npm package or creating own function to generate json file from elements in this class: I have issues with finding npm package or creating own function to generate json file from elements in this class:
export class TranslatedFileElement {
private key: string
private hasChild: boolean // True if vaule is empty, vaules not empty; False if value not empty, values is empty
private value?: string // One of them should be required, because if TranslatedFileElement have any childs,
private values?: TranslatedFileElement[] // then value should be empty, if not, values should be empty
private isTranslated?: boolean
public constructor() {
this.key = '',
this.hasChild = false,
this.value = '',
this.values = null,
this.isTranslated = false
}
public setTranslateFileElement(
_key: string,
_value: string,
_values: TranslatedFileElement[],
_hasChild: boolean,
_isTranslated: boolean
) {
this.key = _key
this.value = _value
this.values = _values
this.hasChild = _hasChild,
this.isTranslated = _isTranslated
}
public setKey(_key: string) {
this.key = _key
}
[...] //Other get's and set's
}
I tried something like this, but it not working properly and generating more problems than solving:我尝试过这样的事情,但它不能正常工作并且产生的问题比解决的问题更多:
private converter(elementsToJSON: TranslatedFileElement[], nestedLevel: number = 0): string {
let JSONResult = '{'
elementsToJSON.forEach((element) => {
JSONResult = JSONResult + '"' + element.getKey() + '" : '
if (element.getHasChild()) {
JSONResult = JSONResult +
this.converter(element.getValues(), nestedLevel + 2)
} else {
JSONResult = JSONResult + '"' + element.getValue() + '",'
}
})
JSONResult = JSONResult + '},'
JSONResult = JSON.parse(JSON.stringify(JSONResult));
return JSONResult
}
Somebody know good npm (not deprecated) package to do this or have idea to simply solve it?有人知道好的 npm (未弃用) package 这样做还是有想法简单地解决它?
I would suggest adding a method that would translate this structure to an object that has same structure as json that you want and calling JSON.stringify on object thusly created. I would suggest adding a method that would translate this structure to an object that has same structure as json that you want and calling JSON.stringify on object thusly created.
class TranslatedFileElement {
...
public toJSON(){
return JSON.stringify(this.toObject());
}
public toObject(){
let val:any=null;
if (this.hasChild){
const reducer = (acc:any, val:any) => ({...acc,...val});
if (this.values)
val = this.values.map((child:TranslatedFileElement)=>child.toObject()).reduce(reducer,{});
}else{
val = this.value;
}
return {[this.key]:val};
}
...
}
What you can do is simply call你能做的就是打电话
JSON.stringify(elementsToJSON);
The class properties will get translated into JSON. class 属性将转换为 JSON。
Only problem would be if The structure of your data is not a tree and has cycles in it.唯一的问题是如果您的数据结构不是树并且其中有循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.