如何检查 JSON 字符串在 JavaScript 中是否有值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18363618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:38:08  来源:igfitidea点击:

How to check if a JSON string has a value in JavaScript?

javascript

提问by BalaKrishnan?

Is there is any way to check if the json string has the value(char or string)? Here is the example:

有没有办法检查json字符串是否具有值(字符或字符串)?这是示例:

{
    "firstName": "John",
    "lastName": "Smith",
    "age": 25,
    "address": {
        "streetAddress": "21 2nd Street",
        "city": "New York",
        "state": "NY",
        "postalCode": 10021
    }
}

I have to check if this json has "m". It must know that "m" exists in a value.

我必须检查这个json是否有“m”。它必须知道“m”存在于一个值中。

回答by Moazzam Khan

use this method, if you have json string, you can use json = $.parseJSON(jsonStr)to parse -

使用这个方法,如果你有json字符串,你可以json = $.parseJSON(jsonStr)用来解析——

function checkForValue(json, value) {
    for (key in json) {
        if (typeof (json[key]) === "object") {
            return checkForValue(json[key], value);
        } else if (json[key] === value) {
            return true;
        }
    }
    return false;
}

回答by djheru

Assuming that the JSON object is assigned to var user

假设将 JSON 对象分配给 var 用户

if(JSON.stringify(user).indexOf('m') > -1){  }

Sorry, upon reading new comments I see you're only looking to see if the string is in a key only. I thought you were looking for an 'm' in the entire JSON (as a string)

抱歉,在阅读新评论时,我发现您只想查看字符串是否仅在键中。我以为你在整个 JSON 中寻找一个“m”(作为一个字符串)

回答by tomaroo

Assuming you get your object syntax corrected, you can loop through the properties in an object by using a forloop:

假设您纠正了对象语法,您可以使用循环遍历对象中的属性for

for(props in myObj) {
    if(myObj[props] === "m") { doSomething(); }
}

回答by eatonphil

Possibly something like this?

可能是这样的?

function parse_json(the_json, char_to_check_for)
{
    try {
        for (var key in the_json) {
            var property = the_json.hasOwnProperty(key);
            return parse_json(property);
        }
    }
    catch { // not json
        if (the_json.indexof(char_to_check_for) !=== -1)
        {
             return true;
        }
        return false;

    }
}

if (parse_json(my_json,'m'))
{
    alert("m is in my json!");
}

回答by Gibolt

If looking in one layer and not a substring:

如果查看一层而不是子字符串:

const hasValue = Object.values(obj).includes("bar");

If looking in one layer for a substring, and no objects as values:

如果在一层中查找子字符串,并且没有对象作为值:

const hasChar = Object.values(obj).join("").includes("m");

If looking in multi-layer for a substring:

如果在多层中查找子字符串:

const list = Object.values(a);
for (let i = 0; i < list.length; i++) {
    const object = list[i];
    if (typeof object === "object") {
        list.splice(i, 1); // Remove object from array
        list = list.concat(Object.values(object)); // Add contents to array
    }
}
// It is important to join by character not in the search substring
const hasValue = list.join("_").includes("m");

NOTE: If searching for a key instead, check this post

注意:如果要搜索密钥,请查看此帖子