在JavaScript中,可以使用hasOwnProperty()方法来判断JSON对象中的字段是否存在。下面是一个示例:

var json = {
  "name": "John",
  "age": 30,
  "city": "New York"
};

if (json.hasOwnProperty("name")) {
  console.log("The 'name' field exists in the JSON object.");
} else {
  console.log("The 'name' field does not exist in the JSON object.");
}

输出结果将是:

The 'name' field exists in the JSON object.

你可以根据需要更改字段名称来判断其他字段是否存在。

javascript判断json字段是否存在

原文地址: https://www.cveoy.top/t/topic/iKz0 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录