Reputation: 1578
I'm trying to get "ID":
s and "children":
, I kind of can now because lots of people from StackOverflow helped me, thank you, but now I'd like to search for IDs and children in that sequence. For now I can only get "ID":
s or "children":
s not both, how can I do it, any help is appreciated?
What I'm getting:
1,3,2
What I'd like to get
1,children,3,2
My code is right below:
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*/ );
"Live long and prosper"
Thanks in advance.
Upvotes: 0
Views: 86
Reputation: 1162
The best way is parsing your string to a 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 );
Upvotes: 2