开发者

JSON parsing and printing JSON object with variable name

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜