如何检查JSON字符串在JavaScript中是否具有值? [英] How to check if a JSON string has a value in JavaScript?

查看:82
本文介绍了如何检查JSON字符串在JavaScript中是否具有值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法检查json字符串是否有值(char或string)?以下是示例:

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

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

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

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

推荐答案

使用此方法,如果你有json字符串,你可以使用 json = $ .parseJSON(jsonStr)要解析 -

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

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;
}

这篇关于如何检查JSON字符串在JavaScript中是否具有值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆