/ 设置 10 秒超时 // 每日统计清 0 runtime_set('todaycomments', 0); runtime_set('todayarticles', 0); runtime_set('todayusers', 0); if ($forumlist) { $fidarr = array(); foreach ($forumlist as $fid => $forum) { $fidarr[] = $forum['fid']; } forum_update($fidarr, array('todayposts' => 0, 'todaythreads' => 0)); } // 清理临时附件 attach_gc(); // 当天24点 $today = strtotime(date('Ymd')) + 86400; runtime_set('cron_2_last_date', $today, TRUE); // 往前推8个小时,尽量保证在前一天 升级过来和采集的数据会很卡 // table_day_cron($time - 8 * 3600); cache_delete('cron_lock_2'); } } } ?>get root json element in javascript - Stack Overflow|Concepts Of Algorithm

get root json element in javascript - Stack Overflow

admin2025-04-19  3

This is my json ... using javascript .. i want to output just the name of the root in this case services then traverse through the individual elements in the array

{
    services: {
        46: {
            servicetypeid: "27",
            serviceid: "51",
            servicename: "Parking",
            description: "Parking Related Payments",
            optioncode: [],
            inputid: [],
            price: [],
            categoryidentifier: []
        },
        47: {
            servicetypeid: "27",
            serviceid: "52",
            servicename: "Markets",
            description: "Markets Related Payments",
            optioncode: [],
            inputid: [],
            price: [],
            categoryidentifier: []
        },
        48: {
            servicetypeid: "27",
            serviceid: "53",
            servicename: "PSV",
            description: "Public Service Vehicles",
            optioncode: [],
            inputid: [],
            price: [],
            categoryidentifier: []
        }
    }
}

This is my json ... using javascript .. i want to output just the name of the root in this case services then traverse through the individual elements in the array

{
    services: {
        46: {
            servicetypeid: "27",
            serviceid: "51",
            servicename: "Parking",
            description: "Parking Related Payments",
            optioncode: [],
            inputid: [],
            price: [],
            categoryidentifier: []
        },
        47: {
            servicetypeid: "27",
            serviceid: "52",
            servicename: "Markets",
            description: "Markets Related Payments",
            optioncode: [],
            inputid: [],
            price: [],
            categoryidentifier: []
        },
        48: {
            servicetypeid: "27",
            serviceid: "53",
            servicename: "PSV",
            description: "Public Service Vehicles",
            optioncode: [],
            inputid: [],
            price: [],
            categoryidentifier: []
        }
    }
}
Share Improve this question edited Aug 6, 2012 at 10:29 Esailija 140k23 gold badges279 silver badges328 bronze badges asked Aug 6, 2012 at 10:28 kmarimakmarima 752 silver badges10 bronze badges 2
  • services is not an array but an object.. you should use an array instead – Esailija Commented Aug 6, 2012 at 10:30
  • possible duplicate of How do I enumerate the properties of a javascript object? – Felix Kling Commented Aug 6, 2012 at 11:14
Add a ment  | 

1 Answer 1

Reset to default 4

That's not JSON format data, you can loop thru the object: To get the root key, you could do:

var rootKey;
for(var prop in tst) {
 console.log( prop ); //will give "services"
 rootKey = prop;
}

And to loop thru all the items:

for( var key in tst[rootKey] ) {
  for( var key1 in tst[rootKey][key] ) {
    console.log( "key:" + key1 + " --- Value:"+ tst[rootKey][key][key1] );
 }
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745001018a279225.html

最新回复(0)