84669 人が学習中
152542 人が学習中
20005 人が学習中
5487 人が学習中
7821 人が学習中
359900 人が学習中
3350 人が学習中
180660 人が学習中
48569 人が学習中
18603 人が学習中
40936 人が学習中
1549 人が学習中
1183 人が学習中
32909 人が学習中
1. 質問: json1 を json2 形式に変換します 2. 説明: プロパティには無限のレイヤーがある可能性があり、タイプはタイプを参照し、配列タイプには項目があり、配列はオブジェクト配列または他のタイプである可能性があります。 。 json1:
2.json2
因为时间关系,注解没写多详细
// 假设 a 就是json1, b 就是json2 var a = { body: {...} }; var b = { body: {...} }; function recursion( data, o, type ) { // 这里规定下,最终递归出来的数据类型 b.body[o] = { 'object': {}, 'array' : [], 'string': '' }[ type ]; // 这里获取下面的新数据, var newData = { 'object': data.properties, 'array' : data.items, 'string': data.description }[ data.type ]; // 判断新数据是否符合递归条件,并获得最终数据 if ( newData && newData.type ) { recursion( newData, o, type ) }else{ if ( type == 'object' || type == 'array' ) { for (let i in newData){ newData[i] = newData[i].description } }; if ( type == 'array' ) { b.body[o].push(newData); } else { b.body[o] = newData; }; }; } var data = a.body.properties; for (let o in data) { // 从这里开始递归, recursion( data[o], o, data[o].type ) } console.log(b);
树形递归遍历就好了。
谢邀。。。。
function deepReadTree(param) { var child_param, cur_result; if (param.properties) { child_param = param.properties; cur_result = {}; for (var key in child_param) { cur_result[key] = deepReadTree(child_param[key]); } } else if (param.items) { child_param = param.items.properties; cur_result = {}; for (var key in child_param) { cur_result.push(deepReadTree(child_param[key])); } }else{ cur_result = param.description; } return cur_result; }
讲道理,你的items应该是个数组,不然出不了你那个数据结构。
因为时间关系,注解没写多详细
树形递归遍历就好了。
谢邀。。。。
讲道理,你的items应该是个数组,不然出不了你那个数据结构。