如何检查节点中嵌套的JSON对象中是否存在密钥?

时间:2021-09-24 19:25:33

I've got the following JSON being sent to the server from the browser:

我从浏览器中将以下JSON发送到服务器:

{
    "title": "Testing again 2",
    "abstract": "An example document",
    "_href": "http://google.com",
    "tags": [ "person" ],
    "attributes": [ {
        "id": 1,
        "type": "TEXT",
        "data": "test"
    } ],
    "sections": [ {
        "id": 1,
        "type": "LIST",
        "data": [ {
            "revision": 124,
            "text": "test"
        } ]
    } ]
}

I need to make sure that the keys "_href", "id" and "revision" are not in the object anyplace at any level.

我需要确保键“_href”,“id”和“revision”不在任何级别的任何位置的对象中。

I found this but it doesn't quite work.

我发现了这个,但它不太有用。

4 个解决方案

#1


1  

you need to parse json then check into the data

你需要解析json然后检查数据

var str = '{
    "title": "Testing again 2",
    "abstract": "An example document",
    "_href": "http://google.com",
    "tags": [ "person" ],
    "attributes": [ {
        "id": 1,
        "type": "TEXT",
        "data": "test"
    } ],
    "sections": [ {
        "id": 1,
        "type": "LIST",
        "data": [ {
            "revision": 124,
            "text": "test"
        } ]
    } ]
}';
var jsonObj = JSON.parse(str);

if ( typeof jsonObj._href == 'undefined') {
    // check 
}

#2


0  

I searched npms.io and found has-any-deep which you can use after JSON.parse ing the JSON.

我搜索了npms.io并找到了在JSON.parse之后你可以使用的任何深度的JSON。

#3


0  

A simple but not 100% foolproof solution would be to parse the JSON to string, and just search for your keys:

一个简单但不是100%万无一失的解决方案是将JSON解析为字符串,并只搜索您的密钥:

var a = JSON.stringify(JSONObject);
var occurs = false;
['"_href"', '"id"', '"version"'].forEach(function(string) {
 if(a.indexOf(string) > -1) occurs = true;
});

The issue of course, is if there are values that match '_href', 'id', 'version' in your JSON. But if you want to use native JS, I guess this is a good bet.

问题当然是,如果在JSON中存在与'_href','id','version'匹配的值。但是如果你想使用原生JS,我想这是一个不错的选择。

var a = {
    "title": "Testing again 2",
    "abstract": "An example document",
   
    "tags": [ "person" ],
    "attributes": [ {
        
        "type": "TEXT",
        "data": "test"
    } ],
    "sections": [ {
        
        "type": "_href asdad",
        "data": [ {
            
            "text": "test"
        } ]
    } ]
},
    
b = {
    "title": "Testing again 2",
    "abstract": "An example document",
    "_href": "http://google.com",
    "tags": [ "person" ],
    "attributes": [ {
        "id": 1,
        "type": "TEXT",
        "data": "test"
    } ],
    "sections": [ {
        "id": 1,
        "type": "LIST",
        "data": [ {
            "revision": 124,
            "text": "test"
        } ]
    } ]
},
aJson = JSON.stringify(a),
bJson = JSON.stringify(b);

var occursa = false, occursb = false;

['"_href"', '"id"', '"version"'].forEach(function(string) {
 if(aJson.indexOf(string) > -1) { occursa = true};
});

['"_href"', '"id"', '"version"'].forEach(function(string) {
 if(bJson.indexOf(string) > -1) { occursb = true};
});

console.log("a");
console.log(occursa);
console.log("b");
console.log(occursb);

#4


0  

You could use the optional second reviver parameter to JSON.parse for this:

您可以将可选的第二个reviver参数用于JSON.parse:

function hasBadProp(json) {
  let badProp = false;

  JSON.parse(json, (k, v) => {
    if ([_href", "id", "revision"].includes(k)) badProp = true;
    return v;
 });

 return badProp;
}

#1


1  

you need to parse json then check into the data

你需要解析json然后检查数据

var str = '{
    "title": "Testing again 2",
    "abstract": "An example document",
    "_href": "http://google.com",
    "tags": [ "person" ],
    "attributes": [ {
        "id": 1,
        "type": "TEXT",
        "data": "test"
    } ],
    "sections": [ {
        "id": 1,
        "type": "LIST",
        "data": [ {
            "revision": 124,
            "text": "test"
        } ]
    } ]
}';
var jsonObj = JSON.parse(str);

if ( typeof jsonObj._href == 'undefined') {
    // check 
}

#2


0  

I searched npms.io and found has-any-deep which you can use after JSON.parse ing the JSON.

我搜索了npms.io并找到了在JSON.parse之后你可以使用的任何深度的JSON。

#3


0  

A simple but not 100% foolproof solution would be to parse the JSON to string, and just search for your keys:

一个简单但不是100%万无一失的解决方案是将JSON解析为字符串,并只搜索您的密钥:

var a = JSON.stringify(JSONObject);
var occurs = false;
['"_href"', '"id"', '"version"'].forEach(function(string) {
 if(a.indexOf(string) > -1) occurs = true;
});

The issue of course, is if there are values that match '_href', 'id', 'version' in your JSON. But if you want to use native JS, I guess this is a good bet.

问题当然是,如果在JSON中存在与'_href','id','version'匹配的值。但是如果你想使用原生JS,我想这是一个不错的选择。

var a = {
    "title": "Testing again 2",
    "abstract": "An example document",
   
    "tags": [ "person" ],
    "attributes": [ {
        
        "type": "TEXT",
        "data": "test"
    } ],
    "sections": [ {
        
        "type": "_href asdad",
        "data": [ {
            
            "text": "test"
        } ]
    } ]
},
    
b = {
    "title": "Testing again 2",
    "abstract": "An example document",
    "_href": "http://google.com",
    "tags": [ "person" ],
    "attributes": [ {
        "id": 1,
        "type": "TEXT",
        "data": "test"
    } ],
    "sections": [ {
        "id": 1,
        "type": "LIST",
        "data": [ {
            "revision": 124,
            "text": "test"
        } ]
    } ]
},
aJson = JSON.stringify(a),
bJson = JSON.stringify(b);

var occursa = false, occursb = false;

['"_href"', '"id"', '"version"'].forEach(function(string) {
 if(aJson.indexOf(string) > -1) { occursa = true};
});

['"_href"', '"id"', '"version"'].forEach(function(string) {
 if(bJson.indexOf(string) > -1) { occursb = true};
});

console.log("a");
console.log(occursa);
console.log("b");
console.log(occursb);

#4


0  

You could use the optional second reviver parameter to JSON.parse for this:

您可以将可选的第二个reviver参数用于JSON.parse:

function hasBadProp(json) {
  let badProp = false;

  JSON.parse(json, (k, v) => {
    if ([_href", "id", "revision"].includes(k)) badProp = true;
    return v;
 });

 return badProp;
}