如何检查 JavaScript 对象是否为 JSON

How to check if JavaScript object is JSON

本文关键字:对象 是否 JSON JavaScript 何检查 检查      更新时间:2023-09-26

我有一个嵌套的JSON对象,我需要循环访问,每个键的值可以是字符串,JSON数组或其他JSON对象。根据对象的类型,我需要执行不同的操作。有什么方法可以检查对象的类型以查看它是字符串,JSON对象还是JSON数组?

我尝试使用 typeofinstanceof但似乎都不起作用,因为typeof会为 JSON 对象和数组返回一个对象,并且当我obj instanceof JSONinstanceof给出错误。

更具体地说,将 JSON 解析为 JS 对象后,有什么方法可以检查它是普通字符串,还是具有键和值的对象(来自 JSON 对象),还是数组(来自 JSON 数组)?

例如:

杰伦

var data = "{'hi':
             {'hello':
               ['hi1','hi2']
             },
            'hey':'words'
           }";

示例 JavaScript

var jsonObj = JSON.parse(data);
var path = ["hi","hello"];
function check(jsonObj, path) {
    var parent = jsonObj;
    for (var i = 0; i < path.length-1; i++) {
        var key = path[i];
        if (parent != undefined) {
            parent = parent[key];
        }
    }
    if (parent != undefined) {
        var endLength = path.length - 1;
        var child = parent[path[endLength]];
        //if child is a string, add some text
        //if child is an object, edit the key/value
        //if child is an array, add a new element
        //if child does not exist, add a new key/value
    }
}

如何进行如上所示的对象检查?

我会检查构造函数属性。

例如

var stringConstructor = "test".constructor;
var arrayConstructor = [].constructor;
var objectConstructor = ({}).constructor;
function whatIsIt(object) {
    if (object === null) {
        return "null";
    }
    if (object === undefined) {
        return "undefined";
    }
    if (object.constructor === stringConstructor) {
        return "String";
    }
    if (object.constructor === arrayConstructor) {
        return "Array";
    }
    if (object.constructor === objectConstructor) {
        return "Object";
    }
    {
        return "don't know";
    }
}
var testSubjects = ["string", [1,2,3], {foo: "bar"}, 4];
for (var i=0, len = testSubjects.length; i < len; i++) {
    alert(whatIsIt(testSubjects[i]));
}

编辑:添加了空检查和未定义检查。

您可以使用 Array.isArray 来检查数组。 然后 typeof obj == 'string',typeof obj == 'object'

var s = 'a string', a = [], o = {}, i = 5;
function getType(p) {
    if (Array.isArray(p)) return 'array';
    else if (typeof p == 'string') return 'string';
    else if (p != null && typeof p == 'object') return 'object';
    else return 'other';
}
console.log("'s' is " + getType(s));
console.log("'a' is " + getType(a));
console.log("'o' is " + getType(o));
console.log("'i' is " + getType(i));

's' 是字符串
'a' 是数组
'o' 是对象
'i' 是其他

JSON 对象

是一个对象。若要检查类型是否为对象类型,请计算构造函数属性。

function isObject(obj)
{
    return obj !== undefined && obj !== null && obj.constructor == Object;
}

这同样适用于所有其他类型的类型:

function isArray(obj)
{
    return obj !== undefined && obj !== null && obj.constructor == Array;
}
function isBoolean(obj)
{
    return obj !== undefined && obj !== null && obj.constructor == Boolean;
}
function isFunction(obj)
{
    return obj !== undefined && obj !== null && obj.constructor == Function;
}
function isNumber(obj)
{
    return obj !== undefined && obj !== null && obj.constructor == Number;
}
function isString(obj)
{
    return obj !== undefined && obj !== null && obj.constructor == String;
}
function isInstanced(obj)
{
    if(obj === undefined || obj === null) { return false; }
    if(isArray(obj)) { return false; }
    if(isBoolean(obj)) { return false; }
    if(isFunction(obj)) { return false; }
    if(isNumber(obj)) { return false; }
    if(isObject(obj)) { return false; }
    if(isString(obj)) { return false; }
    return true;
}
你也可以

尝试解析数据,然后检查你是否得到了对象:

try {
    var testIfJson = JSON.parse(data);
    if (typeof testIfJson == "object"){
        //Json
    } else {
        //Not Json
    }
}
catch {
    return false;
}

如果您在解析JSON字符串后尝试检查object的类型,我建议您检查构造函数属性:

obj.constructor == Array || obj.constructor == String || obj.constructor == Object

这将是一个比typeof或instanceof更快的检查。

如果 JSON 库不返回使用这些函数构造的对象,我会非常怀疑它。

@PeterWilkinson的答案对我不起作用,因为"类型化"对象的构造函数是根据该对象的名称自定义的。我不得不使用类型

function isJson(obj) {
    var t = typeof obj;
    return ['boolean', 'number', 'string', 'symbol', 'function'].indexOf(t) == -1;
}

您可以创建自己的构造函数来进行 JSON 解析:

var JSONObj = function(obj) { $.extend(this, JSON.parse(obj)); }
var test = new JSONObj('{"a": "apple"}');
//{a: "apple"}

然后检查实例以查看它最初是否需要解析

test instanceof JSONObj

我写了一个 npm 模块来解决这个问题。可在此处获得:

object-types:一个模块,用于查找对象下面的文字类型

安装

  npm install --save object-types


用法

const objectTypes = require('object-types');
objectTypes({});
//=> 'object'
objectTypes([]);
//=> 'array'
objectTypes(new Object(true));
//=> 'boolean'

看看,它应该可以解决您的确切问题。如果您有任何问题,请告诉我!https://github.com/dawsonbotsford/object-types

尝试,捕获块将帮助您解决此问题

创建函数

function IsJson(str) {
    try {
        JSON.parse(str);
    } catch (e) {
        return false;
    }
    return true;
}

例:

console.log(IsJson('abc')) // false
console.log(IsJson('[{"type":"email","detail":"john@example.com"}]')) // true

为什么不检查数字 - 有点短,可以在IE/Chrome/FF/node中使用.js

function whatIsIt(object) {
    if (object === null) {
        return "null";
    }
    else if (object === undefined) {
        return "undefined";
    }
    if (object.constructor.name) {
            return object.constructor.name;
    }
    else { // last chance 4 IE: "'nfunction Number() {'n    [native code]'n}'n" / node.js: "function String() { [native code] }"
        var name = object.constructor.toString().split(' ');
        if (name && name.length > 1) {
            name = name[1];
            return name.substr(0, name.indexOf('('));
        }
        else { // unreachable now(?)
            return "don't know";
        }
    }
}
var testSubjects = ["string", [1,2,3], {foo: "bar"}, 4];
// Test all options
console.log(whatIsIt(null));
console.log(whatIsIt());
for (var i=0, len = testSubjects.length; i < len; i++) {
    console.log(whatIsIt(testSubjects[i]));
}

我将typeof运算符与构造函数属性的检查(由Peter)结合使用:

var typeOf = function(object) {
    var firstShot = typeof object;
    if (firstShot !== 'object') {
        return firstShot;
    } 
    else if (object.constructor === [].constructor) {
        return 'array';
    }
    else if (object.constructor === {}.constructor) {
        return 'object';
    }
    else if (object === null) {
        return 'null';
    }
    else {
        return 'don''t know';
    } 
}
// Test
var testSubjects = [true, false, 1, 2.3, 'string', [4,5,6], {foo: 'bar'}, null, undefined];
console.log(['typeOf()', 'input parameter'].join(''t'))
console.log(new Array(28).join('-'));
testSubjects.map(function(testSubject){
    console.log([typeOf(testSubject), JSON.stringify(testSubject)].join(''t't'));
});

结果:

typeOf()    input parameter
---------------------------
boolean     true
boolean     false
number      1
number      2.3
string      "string"
array       [4,5,6]
object      {"foo":"bar"}
null        null
undefined       
我知道

这是一个非常古老的问题,答案很好。但是,似乎仍然可以将我的 2¢ 添加到其中。

假设您尝试测试的不是 JSON 对象本身,而是格式化为 JSON 的字符串(在您的var data中似乎是这种情况),您可以使用以下返回布尔值的函数(是或不是"JSON"):

function isJsonString( jsonString ) {
  // This function below ('printError') can be used to print details about the error, if any.
  // Please, refer to the original article (see the end of this post)
  // for more details. I suppressed details to keep the code clean.
  //
  let printError = function(error, explicit) {
  console.log(`[${explicit ? 'EXPLICIT' : 'INEXPLICIT'}] ${error.name}: ${error.message}`);
  }

  try {
      JSON.parse( jsonString );
      return true; // It's a valid JSON format
  } catch (e) {
      return false; // It's not a valid JSON format
  }
}

以下是使用上述函数的一些示例:

console.log(''n1 -----------------');
let j = "abc";
console.log( j, isJsonString(j) );
console.log(''n2 -----------------');
j = `{"abc": "def"}`;
console.log( j, isJsonString(j) );
console.log(''n3 -----------------');
j = '{"abc": "def}';
console.log( j, isJsonString(j) );
console.log(''n4 -----------------');
j = '{}';
console.log( j, isJsonString(j) );
console.log(''n5 -----------------');
j = '[{}]';
console.log( j, isJsonString(j) );
console.log(''n6 -----------------');
j = '[{},]';
console.log( j, isJsonString(j) );
console.log(''n7 -----------------');
j = '[{"a":1, "b":   2}, {"c":3}]';
console.log( j, isJsonString(j) );

当您运行上面的代码时,您将获得以下结果:

1 -----------------
abc false
2 -----------------
{"abc": "def"} true
3 -----------------
{"abc": "def} false
4 -----------------
{} true
5 -----------------
[{}] true
6 -----------------
[{},] false
7 -----------------
[{"a":1, "b":   2}, {"c":3}] true

请尝试下面的代码片段,让我们知道这是否适合您。 :)

重要提示:本文中介绍的函数改编自 https://airbrake.io/blog/javascript-error-handling/syntaxerror-json-parse-bad-parsing,您可以在其中找到有关 JSON.parse() 函数的更多有趣的细节。

function isJsonString( jsonString ) {
  let printError = function(error, explicit) {
  console.log(`[${explicit ? 'EXPLICIT' : 'INEXPLICIT'}] ${error.name}: ${error.message}`);
  }
  try {
      JSON.parse( jsonString );
      return true; // It's a valid JSON format
  } catch (e) {
      return false; // It's not a valid JSON format
  }
}
console.log(''n1 -----------------');
let j = "abc";
console.log( j, isJsonString(j) );
console.log(''n2 -----------------');
j = `{"abc": "def"}`;
console.log( j, isJsonString(j) );
console.log(''n3 -----------------');
j = '{"abc": "def}';
console.log( j, isJsonString(j) );
console.log(''n4 -----------------');
j = '{}';
console.log( j, isJsonString(j) );
console.log(''n5 -----------------');
j = '[{}]';
console.log( j, isJsonString(j) );
console.log(''n6 -----------------');
j = '[{},]';
console.log( j, isJsonString(j) );
console.log(''n7 -----------------');
j = '[{"a":1, "b":   2}, {"c":3}]';
console.log( j, isJsonString(j) );

试试这个

if ( typeof is_json != "function" )
function is_json( _obj )
{
    var _has_keys = 0 ;
    for( var _pr in _obj )
    {
        if ( _obj.hasOwnProperty( _pr ) && !( /^'d+$/.test( _pr ) ) )
        {
           _has_keys = 1 ;
           break ;
        }
    }
    return ( _has_keys && _obj.constructor == Object && _obj.constructor != Array ) ? 1 : 0 ;
}

它适用于下面的示例

var _a = { "name" : "me",
       "surname" : "I",
       "nickname" : {
                      "first" : "wow",
                      "second" : "super",
                      "morelevel" : {
                                      "3level1" : 1,
                                      "3level2" : 2,
                                      "3level3" : 3
                                    }
                    }
     } ;
var _b = [ "name", "surname", "nickname" ] ;
var _c = "abcdefg" ;
console.log( is_json( _a ) );
console.log( is_json( _b ) );
console.log( is_json( _c ) );

基于@Martin Wantke 的答案,但有一些建议的改进/调整......

// NOTE: Check JavaScript type. By Questor
function getJSType(valToChk) {
    function isUndefined(valToChk) { return valToChk === undefined; }
    function isNull(valToChk) { return valToChk === null; }
    function isArray(valToChk) { return valToChk.constructor == Array; }
    function isBoolean(valToChk) { return valToChk.constructor == Boolean; }
    function isFunction(valToChk) { return valToChk.constructor == Function; }
    function isNumber(valToChk) { return valToChk.constructor == Number; }
    function isString(valToChk) { return valToChk.constructor == String; }
    function isObject(valToChk) { return valToChk.constructor == Object; }
    if(isUndefined(valToChk)) { return "undefined"; }
    if(isNull(valToChk)) { return "null"; }
    if(isArray(valToChk)) { return "array"; }
    if(isBoolean(valToChk)) { return "boolean"; }
    if(isFunction(valToChk)) { return "function"; }
    if(isNumber(valToChk)) { return "number"; }
    if(isString(valToChk)) { return "string"; }
    if(isObject(valToChk)) { return "object"; }
}

注意:我发现这种方法非常有教意义,所以我提交了这个答案。

彼得的回答加上额外的检查!当然,不是100%保证!

var isJson = false;
outPutValue = ""
var objectConstructor = {}.constructor;
if(jsonToCheck.constructor === objectConstructor){
    outPutValue = JSON.stringify(jsonToCheck);
    try{
            JSON.parse(outPutValue);
            isJson = true;
    }catch(err){
            isJson = false;
    }
}
if(isJson){
    alert("Is json |" + JSON.stringify(jsonToCheck) + "|");
}else{
    alert("Is other!");
}

我对此有一个非常懒惰的答案,如果您尝试解析字符串/其他值,它不会引发错误。

const checkForJson = (value) => {
    if (typeof value !== "string") return false;
    return value[0] === "{" && value[value.length - 1] === "}";
}

您可以使用它来检查密钥的值,同时进行一些递归函数;抱歉,如果这不能完全回答问题

Ofc 这不是最优雅的解决方案,当字符串实际上以"{"开头并以"}"结尾时会失败,尽管这些用例很少见,如果您真的想要,您可以检查是否存在引号或其他废话......无论如何,请自行决定使用。

TLDR:它不是防弹的,但它很简单,适用于绝大多数用例。

lodash也是检查这些东西的最佳选择。

function Foo() {
  this.a = 1;
}
 
_.isPlainObject(new Foo);
// => false
 
_.isPlainObject([1, 2, 3]);
// => false
 
_.isPlainObject({ 'x': 0, 'y': 0 });
// => true
 
_.isPlainObject(Object.create(null));
// => true


https://www.npmjs.com/package/lodashhttps://lodash.com/docs/#isPlainObject

使用 lodash-contrib 快速检查 JSON 结构:

const _ = require('lodash-contrib');
_.isJSON('{"car": "ferarri"}'); //true for stringified
_.isJSON({car: "ferarri"}); //true  

使用指南:此博客条目

试试这种肮脏的方式

 ('' + obj).includes('{')