简体   繁体   中英

Test Json does not work

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...

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM