Bader H Al Rayyes
Bader H Al Rayyes

Reputation: 522

call an element inside json object with jquery

i am trying to call an element inside a json object, here is a part of the code

{
    " academy": {
        "business": {
            "E-commerce": [

now i successed to call academy as the first element, by this code

$.getJSON("professorUnversityCollegeCourseData.js", function(property) {
$.each(property, function (key, value) {
    $('#uni').add('<option value="'+ key + '" id="'+ count +'">'+ key +  
                  '</option>').appendTo('#university-selection');
    arrayUni[count] = key;
    count++;
});

how can i get the "business" element now ?

Upvotes: 0

Views: 1092

Answers (3)

Danilo Valente
Danilo Valente

Reputation: 11352

Have you ever tried:

$.getJSON("professorUnversityCollegeCourseData.js", function(property) {
    $.each(property, function (key, value){
        $('#uni').add('<option value="'+ key + '" id="'+ count +'">'+ key + '</option>').appendTo('#university-selection');
        arrayUni[count] = key;
        count++;
        alert(this.academy.business);//Or also this['academy']['business']
    });
    alert(property.academy.business);//Or also property['academy']['business']
});

Upvotes: 1

adeneo
adeneo

Reputation: 318372

I assume this is what you are after

key.academy.business

It really is that simple, or :

key['academy'] 

Upvotes: 1

Derek 朕會功夫
Derek 朕會功夫

Reputation: 94409

I think what you should do is this:

$.getJSON("professorUnversityCollegeCourseData.js", function(property){
    business = property.academy.business;
     //-or-
    business = property["academy"]["business"];
});

(according to the data you put up there.)

Upvotes: 1

Related Questions