开发者

JSON problem solution

I have this fiddle. I want to alert the background. How开发者_如何学JAVA can I?

Thanks.


Two problems:

  1. You must eval the JSON string to get an object.
  2. 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);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜