0

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
  • 1
    Works for me: jsfiddle.net/5LGVe Maybe your bug is elsewhere? Commented Sep 26, 2010 at 17:51

1 Answer 1

1

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

Comments