I'm a beginner with Json, I follow this tutorial the example I have written is based on the tutorial, but I do not understand it does not work :
<script language="javascript">
var Jtext="{"variables":["var1","var2","var3"]}";
var Jobj=eval("(" + Jtext + ")");
var j=Jobj["variables"];
document.write(j[0]);
</script>
Any helps...
2) http://javascriptweblog.wordpress.com/2010/04/19/how-evil-is-eval - Vivek Chandra 2012-04-04 21:05
eval
in Python, I accept that I may end up running a Djano web app on the development server). Turns out you almost never, and when you do, you know exactly and don't have to ask. For anything lesser, there are specifically tailored parsers, dead-simple interpreter, perhaps some regexes, or a different (often cleaner and simpler!) way to rewrite the code - NoName 2012-04-04 21:07
Remove the evil eval and the double quotes on that JSON Obj.
<script>
var obj={"variables":["var1","var2","var3"]};
var j=obj["variables"];
document.write(j[0]);
</script>
What's wrong in Your example:
var Jtext="{"variables":["var1","var2","var3"]}";
This doesn't work. variables
, var1
, var2
, ... are out of the string, like the syntax highlight shows us here. That leads to a Uncaught SyntaxError: Unexpected identifier.
Try:
<script type="text/javascript" language="javascript">
var Jobj = {'variables':['var1','var2','var3']};
var j = Jobj.variables;
document.write(j[0]);
</script>