【发布时间】:2020-04-13 21:28:03
【问题描述】:
我有一个从Collection > Folder/Request > Request 结构生成的 JSON 数组。
示例:
[
{
"name": "My Collection",
"folders": [
{
"name": "bdg",
"requests": [
{
"url": "https://reqres.in",
"path": "/api/users",
"method": "GET",
"auth": "None",
"httpUser": "",
"httpPassword": "",
"passwordFieldType": "password",
"bearerToken": "",
"headers": [],
"params": [],
"bodyParams": [],
"rawParams": "",
"rawInput": false,
"contentType": "application/json",
"requestType": "cURL",
"name": "My Request",
"collection": 0
}
]
}
],
"requests": [
{
"url": "https://reqres.in",
"path": "/api/users",
"method": "GET",
"auth": "None",
"httpUser": "",
"httpPassword": "",
"passwordFieldType": "password",
"bearerToken": "",
"headers": [],
"params": [],
"bodyParams": [],
"rawParams": "",
"rawInput": false,
"contentType": "application/json",
"requestType": "cURL",
"name": "My Request",
"collection": 0
}
]
},
{
"name": "fndo",
"folders": [
{
"name": "bdghg",
"requests": [
{
"url": "https://reqres.in",
"path": "/api/users",
"method": "GET",
"auth": "None",
"httpUser": "",
"httpPassword": "",
"passwordFieldType": "password",
"bearerToken": "",
"headers": [],
"params": [],
"bodyParams": [],
"rawParams": "",
"rawInput": false,
"contentType": "application/json",
"requestType": "cURL",
"name": "My Request",
"collection": 1
}
]
}
],
"requests": [
{
"url": "https://reqres.in",
"path": "/api/users",
"method": "GET",
"auth": "None",
"httpUser": "",
"httpPassword": "",
"passwordFieldType": "password",
"bearerToken": "",
"headers": [],
"params": [],
"bodyParams": [],
"rawParams": "",
"rawInput": false,
"contentType": "application/json",
"requestType": "cURL",
"name": "My Request",
"collection": 1
}
]
}
]
我想遍历这个 JSON 中的所有节点、数组和对象。 JSON 将由对象、对象数组和数组组成。
我尝试了Object.keys()、.map() 和.forEach(),但在嵌套条件下失败了。
具有递归的 E6S 解决方案将成为救命稻草。
提前致谢!
【问题讨论】:
-
I tried- 展示您尝试过的内容,而不仅仅是引用您认为可以使用的方法。 SO 希望您实际上首先尝试解决自己的问题。另外,为什么需要递归?或者数据的深度可以超过 2 层吗? -
我一定错过了
JSON.parse()部分。是的,只有 2 级深度。
标签: javascript arrays json vue.js