views:

55

answers:

1

I'm having trouble printing an object with a variable name. It works when I hard code it.

            var objectVarName = "lat";
            var obj = jQuery.parseJSON(JSON.stringify(msg));

            // {"lat":"93"} is what JSON.stringify(msg) prints

            $('#display').prepend("<br/><br/>" + JSON.stringify(msg));

            //obj['lat'] works, obj[objectVarName] does not
            $('#display').prepend("<br/><br/>" + obj['lat']);
+1  A: 

Double check that your variable name, casing, etc are correct...your code works if msg is a valid object, here's what I tested:

var msg = {"lat":"93"};

You can test/see the result here, I changed .prepend() to .append() so the output is in order, no other changes besides that, the result is:

{"lat":"93"}
93
Nick Craver