title: Easiest way to convert json data into objects with methods attached?
body: <p><strong>What's the quickest and easiest way to convert my json, containing the data of the objects, into actual objects with methods attached?</strong></p>
  
  <p>By way of example, I get data for a fruitbowl with an array of fruit objects which in turn contain an array of seeds thus:</p>
  
  <pre><code>{"fruitbowl": [{
       "name": "apple", 
       "color": "red", 
       "seeds": []
     },{
       "name": "orange", 
       "color": "orange", 
       "seeds": [
          {"size":"small","density":"hard"},
          {"size":"small","density":"soft"}
      ]}
  }
  </code></pre>
  
  <p>That's all nice and good but down on the client we do stuff with this fruit, like eat it and plant trees...</p>
  
  <pre><code> var fruitbowl = []
   function Fruit(name, color, seeds){
       this.name = name
       this.color = color
       this.seeds = seeds
       this.eat = function(){
           // munch munch
       }
   }
   function Seed(size, density){
       this.size = size
       this.density = density
       this.plant = function(){
            // grow grow
       }
   }
  </code></pre>
  
  <p>My ajax's success routine currently is currently looping over the thing and constructing each object in turn and it doesn't handle the seeds yet, because before I go looping over seed constructors I'm thinking</p>
  
  <p><strong>Is there not a better way?</strong></p>
  
  <pre><code>    success: function(data){           
          fruitbowl.length = 0
          $.each(data.fruitbowl, function(i, f){
              fruitbowl.push(new Fruit(f.name, f.color, f.seeds))
          })
  </code></pre>
  
  <p>I haven't explored looping over the objects as they are and attaching all the methods. Would that work?</p>