Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 29th, 2012  |  syntax: None  |  size: 3.28 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. take JSON input and store it in to an array (javascript)
  2. {
  3.     "Categories": {
  4.         "Facets": [{
  5.             "count": 1,
  6.             "entity": "Company",
  7.             "Company": [{
  8.  
  9.                 "entity": "Ford Motor Co",
  10.  
  11.                 "Ford_Motor_Co": [{
  12.                     "count": 1,
  13.                     "entity": "Ford"
  14.                 }]
  15.             }]
  16.         }, {
  17.             "count": 4,
  18.             "entity": "Country",
  19.             "Country": [{
  20.  
  21.                 "entity": "Germany",
  22.                 "Germany": [{
  23.                     "count": 1,
  24.                     "entity": "Germany"
  25.                 }],
  26.                 "currency": "Euro (EUR)"
  27.             }, {
  28.  
  29.                 "entity": "Italy",
  30.                 "Italy": [{
  31.                     "count": 1,
  32.                     "entity": "Italy"
  33.                 }],
  34.                 "currency": "Euro (EUR)"
  35.             }, {
  36.  
  37.                 "entity": "Japan",
  38.                 "Japan": [{
  39.                     "count": 1,
  40.                     "entity": "Japan"
  41.                 }],
  42.                 "currency": "Yen (JPY)"
  43.             }, {
  44.  
  45.                 "entity": "South Korea",
  46.                 "South_Korea": [{
  47.                     "count": 1,
  48.                     "entity": "South Korea"
  49.                 }],
  50.                 "currency": "Won (KRW)"
  51.             }]
  52.         }, {
  53.             "count": 5,
  54.             "entity": "Persons",
  55.             "Persons": [{
  56.                 "count": 2,
  57.                 "entity": "Dodge"
  58.             }, {
  59.                 "count": 1,
  60.                 "entity": "Dodge Avenger"
  61.             }, {
  62.                 "count": 1,
  63.                 "entity": "Major League"
  64.             }, {
  65.                 "count": 1,
  66.                 "entity": "Sterling Heights"
  67.             }]
  68.         }]
  69.  
  70.     }
  71. }
  72.        
  73. [Company, Ford Motor Co, Ford, ....... , Sterling Heights]
  74.        
  75. for (var k in h.Categories.Facets)
  76. {
  77.  
  78. alert(h.Categories.Facets[k].entity);
  79.  
  80. }
  81.        
  82. function getEntities(ent)
  83. {
  84.    alert(ent);
  85.    for (var l in ent)
  86.    {
  87.       getEntities(ent[l].entity);
  88.    }
  89. }
  90.        
  91. for (var k in h.Categories.Facets)
  92. {
  93.    getEntities(h.Categories.Facets[k]);
  94. }
  95.        
  96. function getEntities(any) {
  97.     var entities = [];
  98.     if ('entity' in any​) {
  99.         entities.push(any.entity);
  100.     }
  101.     for (var prop in any) {
  102.         if (any[prop] instanceof Object && any.hasOwnProperty(prop)) {
  103.             entities.append(getEntities(any[prop]));
  104.         }
  105.     }
  106.     return entities;
  107. }
  108. console.log(getEntities(h));
  109.        
  110. if (any[prop] instanceof Object && any.hasOwnProperty(prop)) {
  111.        
  112. var allEntities = [];
  113. function getEntities(obj)
  114. {
  115.    if (obj != null)
  116.    {
  117.        var entityName = obj["entity"];
  118.        alert(entityName);
  119.        if (entityName != null)
  120.        {
  121.            allEntities.push(entityName);
  122.            var adjName = entityName.replace(/ /gi, "_");
  123.            var entities = obj[adjName];
  124.            if (entities != null)
  125.            {
  126.                for (var e in entities)
  127.                {
  128.                    var innerEntities = getEntities(entities[e]);
  129.                    for (var inner in innerEntities)
  130.                        allEntities.push(innerEntities[inner]);
  131.                }
  132.            }
  133.        }
  134.     }
  135. }    
  136.  
  137. for (var k in h.Categories.Facets)
  138. {
  139.     alert(h.Categories.Facets[k].entity);
  140.     getEntities(h.Categories.Facets[k]);
  141. }
  142. alert(allEntities.length);