我正在尝试获取 "ID":
和 "children":
,我现在可以了,因为 StackOverflow 的很多人都帮助了我,谢谢,但现在我想按该顺序搜索 ID 和子项。目前我只能获取 "ID":
或 "children":
不能同时获取,我该怎么做,不胜感激?
我得到了什么:
1,3,2
我想要得到什么
1,children,3,2
我的代码就在下面:
var data = '[{"name":"node1","id":1,"is_open":true,"children":[{"name":"child2","id":3},{"name":"child1","id":2}]}]';
var arrays = [];
var t1 = data.match(/"id":[0-9]+(,|})*/g);
//arrays = t1;
for (var x = 0; x < t1.length; x++) {
arrays.push(t1[x].match(/\d+/));
}
//var t2=data.match(/"children":/g);
alert(arrays /*+"\n\n\n\n\n"+t2*/ );
“长寿与繁荣”
提前致谢。
最佳答案
最好的方法是将字符串解析为 JSON:http://api.jquery.com/jquery.parsejson/
var obj = jQuery.parseJSON( '{"name":"node1","id":1,"is_open":true,"children":[{"name":"child2","id":3},{"name":"child1","id":2}]}' );
console.log( obj.id );
console.log( obj.children[0].id );
console.log( obj.children[1].id );
关于javascript - 如何在一个字符串中查找两个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31699506/