【发布时间】:2021-10-13 18:44:17
【问题描述】:
我需要将从服务器获取的数据导出为 .txt 文件
我得到的结构是这样的:
{
"value": {
"name": "A",
"mandatory": true,
"contentMandatory": true,
"multipleAllowed": false,
"fixed": true
},
"children": [
{
"value": {
"name": "Aa",
"mandatory": false,
"contentMandatory": true,
"multipleAllowed": false,
"mergeStrategy": {
"_type": "PrioritizedSourceStrategy"
},
"fixed": false
},
"children": [
{
"value": {
"name": "Aaa",
"mandatory": false,
"contentMandatory": false,
"multipleAllowed": true,
"fixed": false
},
"children": [
{
"value": {
"name": "AaaA",
"mandatory": true,
"contentMandatory": true,
"multipleAllowed": false,
"fixed": false
},
"children": []
}
]
},
{
"value": {
"name": "AaB",
"mandatory": false,
"contentMandatory": false,
"multipleAllowed": true,
"fixed": false
},
"children": [
{
"value": {
"name": "AaBa",
"mandatory": true,
"contentMandatory": true,
"multipleAllowed": false,
"fixed": false
},
"children": []
}
]
}
]
}
...
}
]
}
但我需要结构在文件中看起来像这样:
"name": "A", "mandatory": true, "contentMandatory": true, "multipleAllowed": false,
"name": "Aa", "mandatory": false, "contentMandatory": true, "multipleAllowed": false, "mergeStrategy": { "_type": "PrioritizedSourceStrategy"}
"name": "Aaa", "mandatory": false, "contentMandatory": false, "multipleAllowed": true,
"name": "AaaA", "mandatory": true, "contentMandatory": true, "multipleAllowed": false,
"name": "AaB", "mandatory": false, "contentMandatory": false, "multipleAllowed": true,
"name": "AaBa", "mandatory": true, "contentMandatory": true, "multipleAllowed": false,
...
所以我需要删除一些对象字段(如“固定”)并显示每个对象一行中留下的字段。 归档此结果的最佳方式是什么?
【问题讨论】:
-
不知道你觉得如何创建 txt 文件,但你可能需要这样的东西:
JSON.stringify(YOUR_DATA).replaceAll('"name": ', '\n"name": ')。这会在每个子字符串 'name: ' 之前添加一个断线 '\n'。
标签: javascript json angular typescript