0

I'm attempting to call values from a JSON associative array. I'm finding difficulties as my object is wrapped in "[ ]". For instance:

var scifi = [
    {
        "Show":"TNG",
        "Ship":"Enterprise",
        "Captain":"Picard"
    },
    {
        "Show":"BSG",
        "Ship":"Galactica",
        "Captain":"Adama"
    },
    {
        "Show":"Firefly",
        "Ship":"Serenity",
        "Captain":"Reynolds"
    }
]

So for instance before I was assuming that in order to call out Adama I would use the command

scifi.Captain[1]

However that seems to be outright failing. Any advice is appreciated.

EDIT-----------

I'm thinking part of the problem may be in the ajax I'm using.

$.ajax({
    url: './php/scifishows.php',
    type: 'POST',
    //dataType: 'json',
    data:
        {
            show: fav_show
        },
    success: function(output)
        {
            alert(output[1].Captain);

        }
});

And this is the php code that causes the brackets, which loops through the mysql results and and places them in a single object. This is of course called by the above ajax.

$all = array();
while( ($row = mysql_fetch_assoc($result)) ) {
    $all[] = $row;
}
3
  • what do you get from a print_r($all) after the while loop is finished? Commented Jun 14, 2013 at 6:35
  • i echo'ed that in the php, and the ajax kicked back nothing. so I'm guessing it caused an error somewhere in the PHP. Just to be sure it wasn't the JS I tried it w Richard's recommended "scifi[1].Captain" format to call the object as well as "scifi" to call the whole object. Both methods yielded nothing. Thanks :) Commented Jun 14, 2013 at 6:45
  • Figured it out. Richard's answer was what I needed, and I was not specifying dataType: "json" in the ajax call. Being sensitive to the single and double quotes. Commented Jun 14, 2013 at 18:50

1 Answer 1

3

[] denotes arrays in JSON, and {}, likewise, denote objects.

So at least in your example, since it's of the form [{},{},...], you have to access by array first, then the object.

// something like
var foo = scifi[1].Captain;

Note that what you have is not an associative array at all (for what definition an "associative array" has in Javascript at least).

To have something akin to an associative array, you'd still use objects:

var scifi = {
    TNG : {
        Ship : 'Enterprise',
        Captain : 'Picard'
    },
    BSG : {
        Ship : 'Galactica',
        Captain : 'Adama'
    },
    Firefly : {
        Ship : 'Serenity',
        Captain : 'Reynolds'
    }
};

Then you'd be able to access that like:

var foo = scifi.TNG.Captain;   // Picard
var bar = scifi.BSG.Ship;      // Galactica

If you really have to work with the format you have, but would like to work with the format I've given, then you can just convert your original data:

var new_scifi = {};
$.each(scifi, function (i,v) {
    new_scifi[v.Show] = {
        Ship = v.Ship,
        Captain = v.Captain
    };
});

console.log(new_scifi.Firefly.Captain);  // Reynolds
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for all of the details. I tried your first suggestion, making "var foo = scifi[1].Captain;" and it just spat out "undefined". I prob don't have to work in this format with the brackets. I am just doing it was the best way I knew to loop through all the mysql results and dump them into an object. I'll attach that code to the top as well. Any other method would be welcome.

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.