I have this fiddle. I want to alert the background. How开发者_如何学JAVA can I?
Thanks.
Two problems:
- You must eval the JSON string to get an object.
- The background value is missing quotes.
It works like this:
var gib = eval("({background:'default.jpg'})");
alert(gib.background)
You're currently just instantiating a string, rather than a JSON object - JSON objects are not surrounded in quotes.
Here's the fixed version:
var gib = { background : "default.jpg "};
alert(gib.background)
You need the json to be valid like this var gib={"background":"default.jpg"}
So yes parse into object first if using jquery jQuery.parseJSON( "{background:default.jpg}" ); alert(gib.background)
Wrong syntax in json.
var gib={background:"default.jpg"};
alert(gib.background);
精彩评论