0

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 ?

3 Answers 3

1

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']
});
Sign up to request clarification or add additional context in comments.

4 Comments

thanks , but i need to know the array number of business as i will complete my script depending on the id= + count + , so if the count number is 1 i will get the some colleges like business,engineering,science (1,2,3), i need to know the number of business and count on it to get all colleges under this academy
Yes, but business is an object, not an array. You can get the name of the property of business with this code: for(var propName in property.business) alert(propName + ': ' + property.business[propName]);
It's an object, it has no array number or length property, maybe it's not an object you are looking for, but an array ?
To treat it as an array you have to write your JSON by this way: { " academy": { "business": [ { name: "E-commerce" }
1

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.)

1 Comment

When calling $.getJSON, jQuery does the JSON parsing for you.
1

I assume this is what you are after

key.academy.business

It really is that simple, or :

key['academy'] 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.