开发者

JSON values exists?

开发者 https://www.devze.com 2023-03-08 19:07 出处:网络
I want to build a script which get s a JSON and manipulates it. I have one problem, i don\'t know how to check if a certain value exists, for example:

I want to build a script which get s a JSON and manipulates it. I have one problem, i don't know how to check if a certain value exists, for example:

i get this JSON when doing action 1:

{
 "url": "http://zuberi.me"开发者_如何学Python,
 "top": "true"
}

and when i do action 2:

{
 "url": "http://zuberi.me",
 "top": "true",
 "copy": "false"
}

so i want to check if "copy" is exists in the JSON response i get...

thanks in advance :)


A javascript implementation:

var json1 = { "url": "http://zuberi.me",
              "top": "true",
              "copy": "false" },

json2 = { "url": "http://zuberi.me",
          "top": "true" };

json1.hasOwnProperty('copy'); // true
json2.hasOwnProperty('copy'); // false


Assuming that your JSON string is already converted to a JavaScript object:

if ("copy" in json) {
  // ...
}


if (typeof(json.copy) === 'boolean') {
0

精彩评论

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