开发者

JSON parsing and printing JSON object with variable name

开发者 https://www.devze.com 2023-01-17 21:19 出处:网络
I\'m having trouble printing an object with a variable name.It works when I hard code it. var objectVarName = \"lat\";

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

            开发者_Go百科//obj['lat'] works, obj[objectVarName] does not
            $('#display').prepend("<br/><br/>" + obj['lat']);


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
0

精彩评论

暂无评论...
验证码 换一张
取 消