JSON values exists?
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') {
精彩评论