json - 解析文本(未知对象的纯字符串),转换为 JSON 并将值提取为 Angular 中的映射(键/值)
问题描述
我有一个类似显示的文本(之后,我删除了反斜杠,并且我美化了 JSON Formmatter)
[
{
"id": "c76f1e77",
"formName": "Form Name",
"description": "Set Form description",
"dependences": [],
"action": 0,
"visible": true,
"horzs": [
{
"id": "425c007a",
"verts": [
{
"id": "934fc845",
"cell": {
"id": "FieldText",
"name": "FieldText",
"description": "Description of FieldText",
"isRequired": true,
"isSensible": false,
"action": 0,
"control": {
"name": "Text",
"description": "Text Field",
"typeData": "text",
"tag": "input"
},
"dependences": [],
"rule": null,
"ruleMessage": null
}
},
{
"id": "9462ee7d",
"cell": {
"id": "FieldNumber",
"name": "FieldNumber",
"description": "Description of Field",
"isRequired": true,
"isSensible": false,
"action": 0,
"control": {
"name": "Numeric",
"description": "Number Field",
"typeData": "number",
"tag": "input"
},
"dependences": [],
"rule": null,
"ruleMessage": null
}
},
{
"id": "bd1ad77b",
"cell": {
"id": "FieldPassword",
"name": "FieldPassword",
"description": "Description of Field",
"isRequired": true,
"isSensible": false,
"action": 0,
"control": {
"name": "Password",
"description": "Password Field",
"typeData": "password",
"tag": "input"
},
"dependences": [],
"rule": null,
"ruleMessage": null
}
}
]
}
]
},
{
"id": "188dc0f3",
"formName": "Form Name",
"description": "Set Form description",
"dependences": [],
"action": 0,
"visible": true,
"horzs": [
{
"id": "d4fe409f",
"verts": [
{
"id": "617ba39c",
"cell": {
"id": "FieldDate",
"name": "FieldDate",
"description": "Description of Field",
"isRequired": true,
"isSensible": false,
"action": 0,
"control": {
"name": "Date",
"description": "Date Field",
"typeData": "date",
"tag": "input"
},
"dependences": [],
"rule": null,
"ruleMessage": null
}
},
{
"id": "9a93e4e3",
"cell": {
"id": "FieldTime",
"name": "FieldTime",
"description": "Description of Field",
"isRequired": true,
"isSensible": false,
"action": 0,
"control": {
"name": "Time",
"description": "Time Field",
"typeData": "time",
"tag": "input"
},
"dependences": [],
"rule": null,
"ruleMessage": null
}
},
{
"id": "43bd5654",
"cell": {
"id": "FieldCheckBox",
"name": "FieldCheckBox",
"description": "Description of Field",
"isRequired": true,
"isSensible": false,
"action": 0,
"control": {
"name": "Checkbox",
"description": "Boolean Field",
"typeData": "checkbox",
"tag": "input"
},
"dependences": [],
"rule": null,
"ruleMessage": null
}
},
{
"id": "d617f5c6",
"cell": {
"id": "FieldRadio",
"name": "FieldRadio",
"description": "Description of Field",
"isRequired": true,
"isSensible": false,
"action": 0,
"control": {
"options": [
{
"id": "Radio1",
"value": "Radio1"
},
{
"id": "Radio2",
"value": "Radio2"
},
{
"id": "Radio3",
"value": "Radio3"
}
],
"name": "Radio",
"description": "Select Boolean Field",
"typeData": "radio",
"tag": "input"
},
"dependences": [],
"rule": null,
"ruleMessage": null
}
}
]
}
]
},
{
"id": "82c2a63a",
"formName": "Form Name",
"description": "Set Form description",
"dependences": [],
"action": 0,
"visible": true,
"horzs": [
{
"id": "e9f242de",
"verts": [
{
"id": "8b75def6",
"cell": {
"id": "FieldList",
"name": "FieldList",
"description": "Description of Field",
"isRequired": true,
"isSensible": false,
"action": 0,
"control": {
"options": [
{
"id": "Item1",
"value": "Item1"
},
{
"id": "Item2",
"value": "Item2"
},
{
"id": "Item3",
"value": "Item3"
}
],
"name": "List de Opciones",
"description": "List Field",
"tag": "list"
},
"dependences": [],
"rule": null,
"ruleMessage": null
}
},
{
"id": "15e4eef5",
"cell": {
"id": "FieldSelect",
"name": "FieldSelect",
"description": "Description of Field",
"isRequired": true,
"isSensible": false,
"action": 0,
"control": {
"options": [
{
"id": "Select1",
"value": "Select1"
},
{
"id": "Select2",
"value": "Select2"
}
],
"name": "List de Opciones",
"description": "Selection Field",
"tag": "select"
},
"dependences": [],
"rule": null,
"ruleMessage": null
}
},
{
"id": "5c50c048",
"cell": {
"id": "FieldTextArea",
"name": "FieldTextArea",
"description": "Description of Field",
"isRequired": true,
"isSensible": false,
"action": 0,
"control": {
"name": "Text",
"description": "TextArea Field",
"typeData": "text",
"tag": "input"
},
"dependences": [],
"rule": null,
"ruleMessage": null
}
}
]
}
]
}
]
如您所见, first 是一个数组,带有unknown objects
. 每个对象都有一个名为 的数组"horzs"
,每个对象内部都有一个名为horz
的数组"verts"
。每个内部vert
都有一个名为的对象"cell"
,每个对象cell
都有一个name
属性和control
属性等。
现在我需要从这个文本中提取所有可能的值-键对:
name
作为键和control
作为值...或
name
作为键和cell
作为值...
作为密钥$[1].horzs[0].verts[1].cell.name
,我得到:
[
"FieldTime"
]
作为$[1].horzs[0].verts[1].cell.control
我得到的价值:
[
{
"name": "Time",
"description": "Time Field",
"typeData": "time",
"tag": "input"
}
]
现在测试$[1].horzs[0].verts.length
我得到:
[
4
]
现在,我需要将 All 存储在字典/Map () 中
我正在尝试使用我的文本,但我不能为每个字符串使用 它怎么办?
解决方案
安装
npm i jsonpath-plus
在您的应用程序中
import {JSONPath} from 'jsonpath-plus';
庞大而丑陋的代码
let map = new Map();
if (source) {
const items = +JSONPath({path: '$.length', json: source});
console.log('items:' + items);
if (items) {
for (let i = 0; i < items; i++) {
const horzsPrefix = '$['+i+'].horzs';
//console.log(horzsPrefix+'.length');
const horzs = +JSONPath({path: horzsPrefix+'.length', json: source});
//console.log('\thorzs:' + horzs);
for (let r = 0; r < horzs; r++) {
const vertsPrefix = horzsPrefix +'['+r+'].verts' ;
//console.log(vertsPrefix+'.length');
const verts = +JSONPath({path: vertsPrefix+'.length', json: source});
//console.log('\t\tverts:' + verts);
for (let c = 0; c < verts; c++) {
const cellPrefix = vertsPrefix + '['+c+'].cell' ;
const namePrefix = cellPrefix + '.name' ;
//console.log(cellPrefix);
const cell = JSONPath({path: cellPrefix, json: source});
//console.log('\t\t\tcell:' + cell);
const name = JSONPath({path: namePrefix, json: source});
console.log('name:' + name);
console.log('cell:');
console.log(cell);
map.set(name, cell);
}
}
}
}
}
console.log('map:');
console.log(map);
更改字符串
if (source) {
const items = +JSONPath({path: '$.length', json: source});
if (items) {
for (let item = 0; item < items; item++) {
const horzsPrefix = `$[${item}].horzs`;
const horzs = +JSONPath({path: `${horzsPrefix}.length`, json: source});
for (let horz = 0; horz < horzs; horz++) {
const vertsPrefix = `${horzsPrefix}[${horz}].verts`;
const verts = +JSONPath({path: `${vertsPrefix}.length`, json: source});
for (let vert = 0; vert < verts; vert++) {
const cellPrefix = `${vertsPrefix}[${vert}].cell`;
const namePrefix = `${cellPrefix}.name`
const cell = JSONPath({path: cellPrefix, json: source});
const name = JSONPath({path: namePrefix, json: source});
console.log('name:');
console.log(name);
console.log('cell:');
console.log(cell);
}
}
}
}
}
或者,Easy 正在使用$...cell
和$...cell.name
推荐阅读
- javascript - Leaflet JS - external controls
- google-apps-script - 必须在每个新副本上启用 Admin SDK
- javascript - 检索两个频道的消息列表
- node.js - 访问 AzureBotStorage 中的所有用户数据
- oauth-2.0 - Maintaining oath callback URLs
- opencv - 为什么 MEAN SHIFT + ORB 没有检测到(所有)多个对象?
- version-control - RTC SCM 查询工作区退出代码
- python - 导入熊猫似乎正在使用已弃用的 python 模块
- ibm-cloud - Watson 自定义情绪
- r - Rmarkdown 不生成 pdf