Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    New Coder
    Join Date
    Jul 2013
    Posts
    48
    Thanks
    9
    Thanked 1 Time in 1 Post

    Recursion with JSON

    Hi to all. I have a JSON code. And can i take path of his childs. Something wih recursion but i cant start with anything.

    http://pastebin.com/Hd8cg1kp

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    What is your question?

    What is it you want to do?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Jul 2013
    Posts
    48
    Thanks
    9
    Thanked 1 Time in 1 Post
    i have a JSON that i paste. What i have to do is, take the path of every child with recursion. In ExtJS comboBox i have to put the every child of the JSON. And when i select one, the path way is has to show below.
    My problem is with RECURSION.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,033
    Thanks
    0
    Thanked 248 Times in 245 Posts
    Here's something for you to start with:
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    </head>
    <body>
    <pre id="debug"></pre>
    <script type="text/javascript">
    (function() {
    	var data = [{
    		"name": "Planning",
    		"id": 1,
    		"parentId": 994423,
    		"children": [{
    			"name": "Investigate",
    			"id": 11,
    			"parentId": 1
    		}, {
    			"name": "Assign resources",
    			"id": 12,
    			"parentId": 1
    		}, {
    			"name": "Gather documents",
    			"id": 13,
    			"parentId": 1
    		}, {
    			"name": "Report to management",
    			"id": 17,
    			"parentId": 1,
    			"children": [{
    				"name": "Implementation Phase 1",
    				"id": 4,
    				"parentId": 17,
    				"children": [{
    					"name": "Preparation work",
    					"id": 34,
    					"parentId": 4
    				}, {
    					"name": "Evaluate chipsets",
    					"id": 14,
    					"parentId": 4
    				}, {
    					"name": "Choose technology suite",
    					"id": 16,
    					"parentId": 4
    				}, {
    					"name": "Build prototype",
    					"id": 15,
    					"parentId": 4,
    					"children": [{
    						"name": "Step 1",
    						"id": 20,
    						"parentId": 15
    					}, {
    						"name": "Step 2",
    						"id": 19,
    						"parentId": 15
    					}, {
    						"name": "Step 3",
    						"id": 18,
    						"parentId": 15
    					}, {
    						"name": "Follow up with customer",
    						"id": 21,
    						"parentId": 15,
    						"children": [{
    							"name": "Customer approval",
    							"id": 5,
    							"parentId": 21
    						}, {
    							"name": "Implementation Phase 2",
    							"id": 6,
    							"parentId": 21,
    							"children": [{
    								"name": "Task 3",
    								"id": 25,
    								"parentId": 6
    							}, {
    								"name": "Task 2",
    								"id": 26,
    								"parentId": 6
    							}, {
    								"name": "Task 1",
    								"id": 27,
    								"parentId": 6,
    								"children": [{
    									"name": "Production phase 1",
    									"id": 8,
    									"parentId": 27,
    									"children": [{
    										"name": "Assemble",
    										"id": 22,
    										"parentId": 8
    									}, {
    										"name": "Load SW",
    										"id": 23,
    										"parentId": 8
    									}, {
    										"name": "Basic testing (inc some test)",
    										"id": 24,
    										"parentId": 8,
    										"children": [{
    											"name": "Customer approval 2",
    											"id": 10,
    											"parentId": 24
    										}]
    									}]
    								}]
    							}]
    						}]
    					}]
    				}]
    			}]
    		}]
    	}, {
    		"name": "Final testing",
    		"id": 9,
    		"parentId": 123321
    	}, {
    		"name": "Delivery",
    		"id": 7,
    		"parentId": 654321
    	}];
    	
    	var debug = document.getElementById('debug');
    		
    	function getChildren(parent) {
    		var children = parent.children;
    		if (children) {
    			for (var j=0; j < children.length; j++) {
    				debug.innerHTML += children[j].name + '\n';
    				getChildren(children[j]);
    			}
    		}
    	}
    	
    	for (var i=0; i < data.length; i++) {
    		getChildren(data[i]);
    	}
    })();
    </script>
    </body>
    </html>

  • Users who have thanked glenngv for this post:

    val10 (09-16-2013)

  • #5
    New Coder
    Join Date
    Jul 2013
    Posts
    48
    Thanks
    9
    Thanked 1 Time in 1 Post
    This is what i need for and how can I find the path to each element.Can i take in object {name:"..." ; path:"..."}.
    Last edited by val10; 09-16-2013 at 01:59 PM.

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,033
    Thanks
    0
    Thanked 248 Times in 245 Posts
    I don't understand what you mean. Can you elaborate?

    Btw, cross-posting is against the forum rules.

    Recursion with JSON

  • #7
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,038
    Thanks
    23
    Thanked 589 Times in 588 Posts
    @val10, post the xml your working with please.
    Evolution - The non-random survival of random variants.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    Now I am really confused. Where did val10 ever mention XML???
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,038
    Thanks
    23
    Thanked 589 Times in 588 Posts
    brain fart. Meant json maybe, i guess. But if it's not about json I meant the other thing.
    sheep eyes here.
    Evolution - The non-random survival of random variants.

  • #10
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,033
    Thanks
    0
    Thanked 248 Times in 245 Posts
    This?
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    </head>
    <body>
    <pre id="debug"></pre>
    <script type="text/javascript">
    (function() {
    	var data = [{
    		"name": "Planning",
    		"id": 1,
    		"parentId": 994423,
    		"children": [{
    			"name": "Investigate",
    			"id": 11,
    			"parentId": 1
    		}, {
    			"name": "Assign resources",
    			"id": 12,
    			"parentId": 1
    		}, {
    			"name": "Gather documents",
    			"id": 13,
    			"parentId": 1
    		}, {
    			"name": "Report to management",
    			"id": 17,
    			"parentId": 1,
    			"children": [{
    				"name": "Implementation Phase 1",
    				"id": 4,
    				"parentId": 17,
    				"children": [{
    					"name": "Preparation work",
    					"id": 34,
    					"parentId": 4
    				}, {
    					"name": "Evaluate chipsets",
    					"id": 14,
    					"parentId": 4
    				}, {
    					"name": "Choose technology suite",
    					"id": 16,
    					"parentId": 4
    				}, {
    					"name": "Build prototype",
    					"id": 15,
    					"parentId": 4,
    					"children": [{
    						"name": "Step 1",
    						"id": 20,
    						"parentId": 15
    					}, {
    						"name": "Step 2",
    						"id": 19,
    						"parentId": 15
    					}, {
    						"name": "Step 3",
    						"id": 18,
    						"parentId": 15
    					}, {
    						"name": "Follow up with customer",
    						"id": 21,
    						"parentId": 15,
    						"children": [{
    							"name": "Customer approval",
    							"id": 5,
    							"parentId": 21
    						}, {
    							"name": "Implementation Phase 2",
    							"id": 6,
    							"parentId": 21,
    							"children": [{
    								"name": "Task 3",
    								"id": 25,
    								"parentId": 6
    							}, {
    								"name": "Task 2",
    								"id": 26,
    								"parentId": 6
    							}, {
    								"name": "Task 1",
    								"id": 27,
    								"parentId": 6,
    								"children": [{
    									"name": "Production phase 1",
    									"id": 8,
    									"parentId": 27,
    									"children": [{
    										"name": "Assemble",
    										"id": 22,
    										"parentId": 8
    									}, {
    										"name": "Load SW",
    										"id": 23,
    										"parentId": 8
    									}, {
    										"name": "Basic testing (inc some test)",
    										"id": 24,
    										"parentId": 8,
    										"children": [{
    											"name": "Customer approval 2",
    											"id": 10,
    											"parentId": 24
    										}]
    									}]
    								}]
    							}]
    						}]
    					}]
    				}]
    			}]
    		}]
    	}, {
    		"name": "Final testing",
    		"id": 9,
    		"parentId": 123321
    	}, {
    		"name": "Delivery",
    		"id": 7,
    		"parentId": 654321
    	}];
    	
    	var debug = document.getElementById('debug');
    		
    	function getChildren(parent, level) {
    		var children = parent.children;
    		if (children) {
    			for (var j=0; j < children.length; j++) {
    				debug.innerHTML += (level > 1 ? new Array(level + 1).join('     '): ' ') + ' |__ ' + children[j].name + '\n';
    				getChildren(children[j], level + 1);
    			}			
    		}
    		else {
    			debug.innerHTML += '\n';
    		}
    	}
    	
    	for (var i=0; i < data.length; i++) {
    		debug.innerHTML += '+ ' + data[i].name + '\n';
    		getChildren(data[i], 1);
    	}
    })();
    </script>
    </body>
    </html>
    This may not exactly what you need but this should give you an idea or something to start with or build upon on.

    Output:
    Code:
    + Planning
      |__ Investigate
    
      |__ Assign resources
    
      |__ Gather documents
    
      |__ Report to management
               |__ Implementation Phase 1
                    |__ Preparation work
    
                    |__ Evaluate chipsets
    
                    |__ Choose technology suite
    
                    |__ Build prototype
                         |__ Step 1
    
                         |__ Step 2
    
                         |__ Step 3
    
                         |__ Follow up with customer
                              |__ Customer approval
    
                              |__ Implementation Phase 2
                                   |__ Task 3
    
                                   |__ Task 2
    
                                   |__ Task 1
                                        |__ Production phase 1
                                             |__ Assemble
    
                                             |__ Load SW
    
                                             |__ Basic testing (inc some test)
                                                  |__ Customer approval 2
    
    + Final testing
    
    + Delivery

  • #11
    New Coder
    Join Date
    Jul 2013
    Posts
    48
    Thanks
    9
    Thanked 1 Time in 1 Post
    Code:
    var tempArr = [],
    data = [{JSON}];
    function getChildren(parent) {
    		var children = parent.children;
            console.log(children);
    		if (children) {
    			for (var j=0; j < children.length; j++) {
                    children[j].fullName = '';
                    if (parent.fullName) {
                        children[j].fullName = parent.fullName + "/";
                    }
                    children[j].fullName += parent.name;
                    
    				tempArr.push(children[j].name);
    				getChildren(children[j]);
    			}
    		}
    	}
    	for (var i=0; i < data.length; i++) {
    		getChildren(data[i]);
    	}
    	console.log(tempArr);
    	
    	Ext.create('Ext.form.ComboBox', {
    		fieldLabel: 'Chose One',
    		store: tempArr,
    		queryMode: 'local',
    		renderTo: Ext.getBody()
    	});

    This is how i get 'name' and 'path' for now, i have path in diffrent location, how i can get it for each name : path ??? When i select something from dropdown to get the path below in text field ???

  • #12
    New Coder
    Join Date
    Jul 2013
    Posts
    48
    Thanks
    9
    Thanked 1 Time in 1 Post
    i dont use XML. I use JSON

  • #13
    Regular Coder
    Join Date
    Aug 2010
    Posts
    966
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Quote Originally Posted by val10 View Post
    how i can get it for each name : path ??? When i select something from dropdown to get the path below in text field ???
    Code:
     
    <!DOCTYPE html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>GetJson</title> 
    <script type="text/javascript">
    //req=new XMLHttpRequest;
    //req.open("GET","json.json",false);
    //req.send(null);
    //jSon=JSON.parse(req.responseText);
    jSon=[{
        "name": "Planning",
        "id": 1,
     "parentId": 994423,
        "children": [{
      "name": "Investigate",
      "id": 11,
      "parentId": 1
     }, {
      "name": "Assign resources",
      "id": 12,
      "parentId": 1
     }, {
      "name": "Gather documents",
      "id": 13,
      "parentId": 1
     }, {
      "name": "Report to management",
      "id": 17,
      "parentId": 1,
      "children": [{
       "name": "Implementation Phase 1",
       "id": 4,
       "parentId": 17,
       "children": [{
        "name": "Preparation work",
        "id": 34,
        "parentId": 4
       }, {
        "name": "Evaluate chipsets",
        "id": 14,
        "parentId": 4
       }, {
        "name": "Choose technology suite",
        "id": 16,
        "parentId": 4
       }, {
        "name": "Build prototype",
        "id": 15,
        "parentId": 4,
        "children": [{
         "name": "Step 1",
         "id": 20,
         "parentId": 15
        }, {
         "name": "Step 2",
         "id": 19,
         "parentId": 15
        }, {
         "name": "Step 3",
         "id": 18,
         "parentId": 15
        }, {
         "name": "Follow up with customer",
         "id": 21,
         "parentId": 15,
         "children": [{
          "name": "Customer approval",
          "id": 5,
          "parentId": 21
         }, {
          "name": "Implementation Phase 2",
          "id": 6,
          "parentId": 21,
          "children": [{
           "name": "Task 3",
           "id": 25,
           "parentId": 6
          }, {
           "name": "Task 2",
           "id": 26,
           "parentId": 6
          }, {
           "name": "Task 1",
           "id": 27,
           "parentId": 6,
           "children": [{
            "name": "Production phase 1",
            "id": 8,
            "parentId": 27,
            "children": [{
             "name": "Assemble",
             "id": 22,
             "parentId": 8
            }, {
             "name": "Load SW",
             "id": 23,
             "parentId": 8
            }, {
             "name": "Basic testing (inc some test)",
             "id": 24,
             "parentId": 8,
             "children": [{
              "name": "Customer approval 2",
              "id": 10,
              "parentId": 24
             }]
            }]
           }]
          }]
         }]
        }]
       }]
      }]
     }]
    }, {
        "name": "Final testing",
        "id": 9,
        "parentId": 123321
    }, {
        "name": "Delivery",
        "id": 7,
        "parentId": 654321
    }];
    </script>
    </head>
    <body>
    <div>
    <select id="selPath" onchange=alert(value)>
    </select>
    </div>
    <script type="text/javascript">
    (function (obj,objName){
     obj.path = objName;
     (function recurseIt(obj){
          for(var a in obj){ 
       if(typeof obj[a] == "object" ){   
        obj[a]["path"] = obj.path+"['"+a+"']";    
        recurseIt(obj[a]);
       }else{
        if(a == "name")selPath.add(new Option(obj[a],obj["path"]))
       }
          }
     })(obj);
    })(jSon,"jSon");
    </script>
    </body>
    </html>
    Last edited by DaveyErwin; 09-17-2013 at 08:23 PM.

  • #14
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,033
    Thanks
    0
    Thanked 248 Times in 245 Posts
    I don't know ExtJS, but here's how I would do it using native JavaScript.
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    </head>
    <body>
    <select id="combobox"></select>
    <input type="text" name="path" id="path" size="200" />
    <script type="text/javascript">
    (function() {
    	var data = [{
    		"name": "Planning",
    		"id": 1,
    		"parentId": 994423,
    		"children": [{
    			"name": "Investigate",
    			"id": 11,
    			"parentId": 1
    		}, {
    			"name": "Assign resources",
    			"id": 12,
    			"parentId": 1
    		}, {
    			"name": "Gather documents",
    			"id": 13,
    			"parentId": 1
    		}, {
    			"name": "Report to management",
    			"id": 17,
    			"parentId": 1,
    			"children": [{
    				"name": "Implementation Phase 1",
    				"id": 4,
    				"parentId": 17,
    				"children": [{
    					"name": "Preparation work",
    					"id": 34,
    					"parentId": 4
    				}, {
    					"name": "Evaluate chipsets",
    					"id": 14,
    					"parentId": 4
    				}, {
    					"name": "Choose technology suite",
    					"id": 16,
    					"parentId": 4
    				}, {
    					"name": "Build prototype",
    					"id": 15,
    					"parentId": 4,
    					"children": [{
    						"name": "Step 1",
    						"id": 20,
    						"parentId": 15
    					}, {
    						"name": "Step 2",
    						"id": 19,
    						"parentId": 15
    					}, {
    						"name": "Step 3",
    						"id": 18,
    						"parentId": 15
    					}, {
    						"name": "Follow up with customer",
    						"id": 21,
    						"parentId": 15,
    						"children": [{
    							"name": "Customer approval",
    							"id": 5,
    							"parentId": 21
    						}, {
    							"name": "Implementation Phase 2",
    							"id": 6,
    							"parentId": 21,
    							"children": [{
    								"name": "Task 3",
    								"id": 25,
    								"parentId": 6
    							}, {
    								"name": "Task 2",
    								"id": 26,
    								"parentId": 6
    							}, {
    								"name": "Task 1",
    								"id": 27,
    								"parentId": 6,
    								"children": [{
    									"name": "Production phase 1",
    									"id": 8,
    									"parentId": 27,
    									"children": [{
    										"name": "Assemble",
    										"id": 22,
    										"parentId": 8
    									}, {
    										"name": "Load SW",
    										"id": 23,
    										"parentId": 8
    									}, {
    										"name": "Basic testing (inc some test)",
    										"id": 24,
    										"parentId": 8,
    										"children": [{
    											"name": "Customer approval 2",
    											"id": 10,
    											"parentId": 24
    										}]
    									}]
    								}]
    							}]
    						}]
    					}]
    				}]
    			}]
    		}]
    	}, {
    		"name": "Final testing",
    		"id": 9,
    		"parentId": 123321
    	}, {
    		"name": "Delivery",
    		"id": 7,
    		"parentId": 654321
    	}];
    	
    	var tempArr = [],
    		combobox = document.getElementById('combobox');
    		
    	function getChildren(parent) {
    		var children = parent.children;
    		if (children) {
    			for (var j = 0; j < children.length; j++) {
    				tempArr.push(children[j].name);
    				combobox.add(new Option(children[j].name, tempArr.join(' / ')));
    				getChildren(children[j]);
    			}			
    		}
    	}
    	
    	for (var i=0; i < data.length; i++) {
    		combobox.add(new Option(data[i].name, data[i].name));
    		tempArr.push(data[i].name);
    		getChildren(data[i]);
    	}
    
    	combobox.onchange = function(e) {
    		document.getElementById('path').value = this.value;
    	}
    
    	combobox.onchange.call(combobox);
    })();
    </script>
    </body>
    </html>

  • #15
    Regular Coder
    Join Date
    Aug 2010
    Posts
    966
    Thanks
    19
    Thanked 211 Times in 209 Posts
    here I assume a different
    meaning for "path" ...

    Code:
     
    <!DOCTYPE html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>GetJson</title> 
    <script type="text/javascript">
    //req=new XMLHttpRequest;
    //req.open("GET","json.json",false);
    //req.send(null);
    //jSon=JSON.parse(req.responseText);
    jSon=[{
        "name": "Planning",
        "id": 1,
     "parentId": 994423,
        "children": [{
      "name": "Investigate",
      "id": 11,
      "parentId": 1
     }, {
      "name": "Assign resources",
      "id": 12,
      "parentId": 1
     }, {
      "name": "Gather documents",
      "id": 13,
      "parentId": 1
     }, {
      "name": "Report to management",
      "id": 17,
      "parentId": 1,
      "children": [{
       "name": "Implementation Phase 1",
       "id": 4,
       "parentId": 17,
       "children": [{
        "name": "Preparation work",
        "id": 34,
        "parentId": 4
       }, {
        "name": "Evaluate chipsets",
        "id": 14,
        "parentId": 4
       }, {
        "name": "Choose technology suite",
        "id": 16,
        "parentId": 4
       }, {
        "name": "Build prototype",
        "id": 15,
        "parentId": 4,
        "children": [{
         "name": "Step 1",
         "id": 20,
         "parentId": 15
        }, {
         "name": "Step 2",
         "id": 19,
         "parentId": 15
        }, {
         "name": "Step 3",
         "id": 18,
         "parentId": 15
        }, {
         "name": "Follow up with customer",
         "id": 21,
         "parentId": 15,
         "children": [{
          "name": "Customer approval",
          "id": 5,
          "parentId": 21
         }, {
          "name": "Implementation Phase 2",
          "id": 6,
          "parentId": 21,
          "children": [{
           "name": "Task 3",
           "id": 25,
           "parentId": 6
          }, {
           "name": "Task 2",
           "id": 26,
           "parentId": 6
          }, {
           "name": "Task 1",
           "id": 27,
           "parentId": 6,
           "children": [{
            "name": "Production phase 1",
            "id": 8,
            "parentId": 27,
            "children": [{
             "name": "Assemble",
             "id": 22,
             "parentId": 8
            }, {
             "name": "Load SW",
             "id": 23,
             "parentId": 8
            }, {
             "name": "Basic testing (inc some test)",
             "id": 24,
             "parentId": 8,
             "children": [{
              "name": "Customer approval 2",
              "id": 10,
              "parentId": 24
             }]
            }]
           }]
          }]
         }]
        }]
       }]
      }]
     }]
    }, {
        "name": "Final testing",
        "id": 9,
        "parentId": 123321
    }, {
        "name": "Delivery",
        "id": 7,
        "parentId": 654321
    }];
    </script>
    </head>
    <body>
    <div>
    <select id="selPath" onchange="dsplyPath.innerHTML=value"></select>
    <textarea id="dsplyPath" rows="5" cols="50"></textarea>
    </div>
    <script type="text/javascript">
    (function (obj,objName){
     obj.names= "";
     (function recurseIt(obj){
          for(var a in obj){ 
       if(typeof obj[a] == "object" ){   
       obj[a].names= obj.names + (obj[a]["name"] ? "/" + obj[a]["name"]:"");
        recurseIt(obj[a]);
       }else{
        if(a == "name")selPath.add(new Option(obj[a],obj["names"]))
       }
          }
     })(obj);
    })(jSon);
    </script>
    </body>
    </html>
    Last edited by DaveyErwin; 09-18-2013 at 12:30 AM.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •