在我的ASP上。NET后端,我返回一个模型数组,称为
Job
,可以有
n
使用信号器的子级作业量。一份工作可能是这样的:
{
"id": 0,
"json": '{"error": "Some error"}',
"children": [{
"id": 1
}, {
"id": 3,
"children": [{
"id": 4,
"json": '{"error": "Some other error"}'
}]
}]
}
正如你所看到的,每个工作都可以有一个孩子,而这个孩子可以有另一个孩子,等等。每个作业还具有
json
属性,它是文本字符串中的JSON。我想将这些反序列化为常规JavaScript对象,如下所示:
var deserialized = {
"id": 0,
"json": {
"error": "Some error"
},
"children": [{
"id": 1
}, {
"id": 3,
"children": [{
"id": 4,
"json": {
"error": "Some other error"
}
}]
}]
}
基本上是这样的:
-
如果作业有
json
属性只需执行
job.json = JSON.parse(job.json)
-
如果工作有子项,则遍历所有子项
-
重复1次
我怎样才能做到这一点?我想递归是一种方法,但我宁愿看看是否有可能利用新的ES6方法。