gpt4 book ai didi

javascript - 复杂的 Javascript 对象解析

转载 作者:行者123 更新时间:2023-11-30 16:39:01 26 4
gpt4 key购买 nike

我有一个 xml 作为 javascript 对象。 xml 看起来像:

<map-Map>
<map-Entry>
<map-Key>
<std-String value="responseMessage"></std-String>
</map-Key>
<map-Value>
<std-String value="success"></std-String>
</map-Value>
</map-Entry>
<map-Entry>
<map-Key>
<std-String value="lastName"></std-String>
</map-Key>
<map-Value>
<std-String value="Page"></std-String>
</map-Value>
</map-Entry>
<map-Entry>
<map-Key>
<std-String value="phone"></std-String>
</map-Key>
<map-Value>
<std-String value="860-634-1602"></std-String>
</map-Value>
</map-Entry>
<map-Entry>
<map-Key>
<std-String value="fax"></std-String>
</map-Key>
<map-Value>
<std-String value="860-429-5183"></std-String>
</map-Value>
</map-Entry>
<map-Entry>
<map-Key>
<std-String value="email"></std-String>
</map-Key>
<map-Value>
<std-String value="larry.page@gmail.com"></std-String>
</map-Value>
</map-Entry>
<map-Entry>
<map-Key>
<std-String value="name"></std-String>
</map-Key>
<map-Value>
<std-String value="Alphabets Inc."></std-String>
</map-Value>
</map-Entry>
<map-Entry>
<map-Key>
<std-String value="altPhone"></std-String>
</map-Key>
<map-Value>
<std-String value="860-429-0021"></std-String>
</map-Value>
</map-Entry>
<map-Entry>
<map-Key>
<std-String value="billingAddress"></std-String>
</map-Key>
<map-Value>
<map-HashMap>
<map-Entry>
<map-Key>
<std-String value="postalCode"></std-String>
</map-Key>
<map-Value>
<std-String value="94043"></std-String>
</map-Value>
</map-Entry>
<map-Entry>
<map-Key>
<std-String value="addr1"></std-String>
</map-Key>
<map-Value>
<std-String value="1600 Amphitheatre Pkwy"></std-String>
</map-Value>
</map-Entry>
<map-Entry>
<map-Key>
<std-String value="addr2"></std-String>
</map-Key>
<map-Value>
<std-String value="Mountain View"></std-String>
</map-Value>
</map-Entry>
<map-Entry>
<map-Key>
<std-String value="state"></std-String>
</map-Key>
<map-Value>
<std-String value="California"></std-String>
</map-Value>
</map-Entry>
<map-Entry>
<map-Key>
<std-String value="country"></std-String>
</map-Key>
<map-Value>
<std-String value="Unites States"></std-String>
</map-Value>
</map-Entry>
<map-Entry>
<map-Key>
<std-String value="city"></std-String>
</map-Key>
<map-Value>
<std-String value="California"></std-String>
</map-Value>
</map-Entry>
</map-HashMap>
</map-Value>
</map-Entry>
<map-Entry>
<map-Key>
<std-String value="dummyList"></std-String>
</map-Key>
<map-Value>
<col-LinkedList>
<std-String value="a"></std-String>
<std-String value="b"></std-String>
<std-String value="c"></std-String>
</col-LinkedList>
</map-Value>
</map-Entry>
<map-Entry>
<map-Key>
<std-String value="firstName"></std-String>
</map-Key>
<map-Value>
<std-String value="Larry"></std-String>
</map-Value>
</map-Entry>
<map-Entry>
<map-Key>
<std-String value="companyName"></std-String>
</map-Key>
<map-Value>
<std-String value="Google"></std-String>
</map-Value>
</map-Entry>
<map-Entry>
<map-Key>
<std-String value="contact"></std-String>
</map-Key>
<map-Value>
<std-String value="sundar.pichai@gmail.com"></std-String>
</map-Value>
</map-Entry>
</map-Map>

我有一个 javascript 对象,看起来像这样:

{
'map-Map':{
'map-Entry':{
'map-Key':{
'std-String':{attributes:{value:'firstName'}}
},
'map-Value':{
'std-String':{attributes:{value:'Larry'}}
}
}
}
};

我想要的是将其转换为简单的键值对,例如:

{'responseMessage':'success', 'lastName':'Page', 'billingAddress':{'postalCode':'321021','addr1':'Mountain View'},'firstName':'Larry'}

请帮助我解析和创建类似这样的东西的逻辑,如果它是通用的,那将会很好,因为节点的嵌套可以在那里。提前致谢。

编辑
尽管我是新手,但我正在尝试使用递归,但这将进入无限循环:

function processedResult(map){
var resultMap = {};
var mapEntry = map['map-Entry'];
for(x=0; x<mapEntry.length; x++){
var value = '';
var child = mapEntry[x];
var key = child['map-Key']['std-String']['attributes']['value'];
var valueNode = child['map-Value'];
if(Object.keys(valueNode)[0] == 'std-String'){
value = valueNode['std-String']['attributes']['value'];
}else if(Object.keys(valueNode)[0] == 'map-HashMap'){
value = processedResult(valueNode['map-HashMap']);
}
resultMap[key] = value;
}
return resultMap;
}

添加整个 JS 对象:

{"map-Entry":[{"map-Key":{"std-String":{"attributes":{"value":"responseMessage"}}},"map-Value":{"std-String":{"attributes":{"value":"success"}}}},{"map-Key":{"std-String":{"attributes":{"value":"lastName"}}},"map-Value":{"std-String":{"attributes":{"value":"Page"}}}},{"map-Key":{"std-String":{"attributes":{"value":"phone"}}},"map-Value":{"std-String":{"attributes":{"value":"860-634-1602"}}}},{"map-Key":{"std-String":{"attributes":{"value":"fax"}}},"map-Value":{"std-String":{"attributes":{"value":"860-429-5183"}}}},{"map-Key":{"std-String":{"attributes":{"value":"email"}}},"map-Value":{"std-String":{"attributes":{"value":"larry.page@gmail.com"}}}},{"map-Key":{"std-String":{"attributes":{"value":"name"}}},"map-Value":{"std-String":{"attributes":{"value":"Alphabets Inc."}}}},{"map-Key":{"std-String":{"attributes":{"value":"altPhone"}}},"map-Value":{"std-String":{"attributes":{"value":"860-429-0021"}}}},{"map-Key":{"std-String":{"attributes":{"value":"billingAddress"}}},"map-Value":{"map-HashMap":{"map-Entry":[{"map-Key":{"std-String":{"attributes":{"value":"postalCode"}}},"map-Value":{"std-String":{"attributes":{"value":"94043"}}}},{"map-Key":{"std-String":{"attributes":{"value":"addr1"}}},"map-Value":{"std-String":{"attributes":{"value":"1600 Amphitheatre Pkwy"}}}},{"map-Key":{"std-String":{"attributes":{"value":"addr2"}}},"map-Value":{"std-String":{"attributes":{"value":"Mountain View"}}}},{"map-Key":{"std-String":{"attributes":{"value":"state"}}},"map-Value":{"std-String":{"attributes":{"value":"California"}}}},{"map-Key":{"std-String":{"attributes":{"value":"country"}}},"map-Value":{"std-String":{"attributes":{"value":"Unites States"}}}},{"map-Key":{"std-String":{"attributes":{"value":"city"}}},"map-Value":{"std-String":{"attributes":{"value":"California"}}}}]}}},{"map-Key":{"std-String":{"attributes":{"value":"dummyList"}}},"map-Value":{"col-LinkedList":{"std-String":[{"attributes":{"value":"a"}},{"attributes":{"value":"b"}},{"attributes":{"value":"c"}}]}}},{"map-Key":{"std-String":{"attributes":{"value":"firstName"}}},"map-Value":{"std-String":{"attributes":{"value":"Larry"}}}},{"map-Key":{"std-String":{"attributes":{"value":"companyName"}}},"map-Value":{"std-String":{"attributes":{"value":"Google"}}}},{"map-Key":{"std-String":{"attributes":{"value":"contact"}}},"map-Value":{"std-String":{"attributes":{"value":"sundar.pichai@gmail.com"}}}}]}  

我在将 JS 对象提供给这个时陷入了无限循环:

var parse = function (node, obj) {
//console.dir(node);
var hashMap;
// ..
if ( Object.keys(node)[0] === "map-Map" ) {
console.log('inside map-Map');
parse(node["map-Map"],obj);
return obj;
}
// ..
if ( Object.keys(node)[0] === "map-HashMap" ) {
console.log('inside map-HashMap');
//console.log(node);
hashMap = {};
_.each(node, function (node) {
console.log(node);
parse(node, hashMap);
});
//parse(node["map-HashMap"]["map-Entry"], hashMap);
return hashMap;
}
// ..
if ( Object.keys(node)[0] === "col-LinkedList" ) {
console.log('inside col-LinkedList');
return _.reduce(node, function (linkedList, node) {
linkedList.push(parse(node, {}));
return linkedList;
}, []);
}
// ..
if ( Object.keys(node)[0] === "map-Entry" ) {
console.log('inside map-Entry');
console.log(node["map-Entry"]);
for(a=0; a<node["map-Entry"].length; a++){
obj[parse(node["map-Entry"][a]["map-Key"], obj)] = parse(node["map-Entry"][a]["map-Value"], obj);
}
//obj[parse(node["map-Key"], obj)] = parse(node["map-Value"], obj);
return obj;
}
// ..
if ( Object.keys(node)[0] === "map-Key" ) {
console.log('inside map-Key');
return parse(node["map-Key"][0], obj);
}
// ..
if ( Object.keys(node)[0] === "map-Value" ) {
console.log('inside map-Value');
return parse(node["map-Value"][0], obj);
}
// ..
if ( Object.keys(node)[0] === "std-String" ) {
console.log('inside std-String');
return node["std-String"]["attributes"]["value"];
}
};

最佳答案

var parse = function (node, obj) {
console.dir(node);
var hashMap;
// ..
if ( node.tagName === "map-Map" ) {
_.each(node.children, function (node) {
parse(node, obj);
});
return obj;
}
// ..
if ( node.tagName === "map-HashMap" ) {
hashMap = {};
_.each(node.children, function (node) {
parse(node, hashMap);
});
return hashMap;
}
// ..
if ( node.tagName === "col-LinkedList" ) {
return _.reduce(node.children, function (linkedList, node) {
linkedList.push(parse(node, {}));
return linkedList;
}, []);
}
// ..
if ( node.tagName === "map-Entry" ) {
obj[parse(node.children[0], obj)] = parse(node.children[1], obj);
return obj;
}
// ..
if ( node.tagName === "map-Key" ) {
return parse(node.children[0], obj);
}
// ..
if ( node.tagName === "map-Value" ) {
return parse(node.children[0], obj);
}
// ..
if ( node.tagName === "std-String" ) {
return node.getAttribute("value");
}
};

var str = '<map-Map><map-Entry><map-Key><std-String value="responseMessage"></std-String></map-Key><map-Value><std-String value="success"></std-String></map-Value></map-Entry><map-Entry><map-Key><std-String value="lastName"></std-String></map-Key><map-Value><std-String value="Page"></std-String></map-Value></map-Entry><map-Entry><map-Key><std-String value="phone"></std-String></map-Key><map-Value><std-String value="860-634-1602"></std-String></map-Value></map-Entry><map-Entry><map-Key><std-String value="fax"></std-String></map-Key><map-Value><std-String value="860-429-5183"></std-String></map-Value></map-Entry><map-Entry><map-Key><std-String value="email"></std-String></map-Key><map-Value><std-String value="larry.page@gmail.com"></std-String></map-Value></map-Entry><map-Entry><map-Key><std-String value="name"></std-String></map-Key><map-Value><std-String value="Alphabets Inc."></std-String></map-Value></map-Entry><map-Entry><map-Key><std-String value="altPhone"></std-String></map-Key><map-Value><std-String value="860-429-0021"></std-String></map-Value></map-Entry><map-Entry><map-Key><std-String value="billingAddress"></std-String></map-Key><map-Value><map-HashMap><map-Entry><map-Key><std-String value="postalCode"></std-String></map-Key><map-Value><std-String value="94043"></std-String></map-Value></map-Entry><map-Entry><map-Key><std-String value="addr1"></std-String></map-Key><map-Value><std-String value="1600 Amphitheatre Pkwy"></std-String></map-Value></map-Entry><map-Entry><map-Key><std-String value="addr2"></std-String></map-Key><map-Value><std-String value="Mountain View"></std-String></map-Value></map-Entry><map-Entry><map-Key><std-String value="state"></std-String></map-Key><map-Value><std-String value="California"></std-String></map-Value></map-Entry><map-Entry><map-Key><std-String value="country"></std-String></map-Key><map-Value><std-String value="Unites States"></std-String></map-Value></map-Entry><map-Entry><map-Key><std-String value="city"></std-String></map-Key><map-Value><std-String value="California"></std-String></map-Value></map-Entry></map-HashMap></map-Value></map-Entry><map-Entry><map-Key><std-String value="dummyList"></std-String></map-Key><map-Value><col-LinkedList><std-String value="a"></std-String><std-String value="b"></std-String><std-String value="c"></std-String></col-LinkedList></map-Value></map-Entry><map-Entry><map-Key><std-String value="firstName"></std-String></map-Key><map-Value><std-String value="Larry"></std-String></map-Value></map-Entry><map-Entry><map-Key><std-String value="companyName"></std-String></map-Key><map-Value><std-String value="Google"></std-String></map-Value></map-Entry><map-Entry><map-Key><std-String value="contact"></std-String></map-Key><map-Value><std-String value="sundar.pichai@gmail.com"></std-String></map-Value></map-Entry></map-Map>';
var parser = new DOMParser();
var xml = parser.parseFromString(str, "text/xml");
console.log(JSON.stringify(parse(xml.children[0], {}), null, 4));

我正在使用 xml 文档和它的节点,但原则应该与解析的 JS obj 相同。尝试通过它并让我知道它是否可以理解。

不确定它是否跨浏览器,应该在 chrome 中工作。

参见 http://devdocs.io/lodash/对于 _.each 和 _.reduce expl。

关于javascript - 复杂的 Javascript 对象解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32294293/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com