开发者

JSON problem solution

开发者 https://www.devze.com 2023-01-29 05:50 出处:网络
I have this fiddle. I want to alert the background. How开发者_如何学JAVA can I? Thanks.Two problems:

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

精彩评论

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

关注公众号