最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check if a string is in a json object - Stack Overflow

programmeradmin6浏览0评论

I'm trying to check if a string is in a JSON object with javascript. I don't know if it is possible or I have to convert something. Here is the part of the code with the if statement in which I want to check if data.userName (the string) is in users (the JSON object)

function validation() {
    var userName_login = document.getElementById("username").value;
    var password_login = document.getElementById("password").value;
    var data = {
        userName: userName_login,
        password: password_login
    };
    doJSONRequest("GET", "/users/", null, data, function(users) {
        if (data.userName) {

        }
    })
}

And the doJSONRequest function is:

function doJSONRequest(method, url, headers, data, callback) {
    if (arguments.length != 5) {
        throw new Error('Illegal argument count');
    }
    doRequestChecks(method, true, data);
    var r = new XMLHttpRequest();
    r.open(method, url, true);
    doRequestSetHeaders(r, method, headers);
    r.onreadystatechange = function() {
        if (r.readyState != 4 || (r.status != 200 && r.status != 201 && r.status != 204)) {
            return;
        } else {
            if (isJSON(r.responseText))
                callback(JSON.parse(r.responseText));
            else
                callback();
        }
    };
    var dataToSend = null;
    if (!("undefined" == typeof data) && !(data === null))
        dataToSend = JSON.stringify(data);
    r.send(dataToSend);
}

I'm trying to check if a string is in a JSON object with javascript. I don't know if it is possible or I have to convert something. Here is the part of the code with the if statement in which I want to check if data.userName (the string) is in users (the JSON object)

function validation() {
    var userName_login = document.getElementById("username").value;
    var password_login = document.getElementById("password").value;
    var data = {
        userName: userName_login,
        password: password_login
    };
    doJSONRequest("GET", "/users/", null, data, function(users) {
        if (data.userName) {

        }
    })
}

And the doJSONRequest function is:

function doJSONRequest(method, url, headers, data, callback) {
    if (arguments.length != 5) {
        throw new Error('Illegal argument count');
    }
    doRequestChecks(method, true, data);
    var r = new XMLHttpRequest();
    r.open(method, url, true);
    doRequestSetHeaders(r, method, headers);
    r.onreadystatechange = function() {
        if (r.readyState != 4 || (r.status != 200 && r.status != 201 && r.status != 204)) {
            return;
        } else {
            if (isJSON(r.responseText))
                callback(JSON.parse(r.responseText));
            else
                callback();
        }
    };
    var dataToSend = null;
    if (!("undefined" == typeof data) && !(data === null))
        dataToSend = JSON.stringify(data);
    r.send(dataToSend);
}
Share Improve this question edited Nov 28, 2014 at 19:26 Mohamad Shiralizadeh 8,7758 gold badges63 silver badges96 bronze badges asked Nov 28, 2014 at 18:49 pp94pp94 1336 silver badges19 bronze badges 5
  • stackoverflow./questions/18363618/… – TheSuper Commented Nov 28, 2014 at 18:51
  • if it's not json, then json.parse will throw an error. do a try/catch. if you catch, then it's not json. – Marc B Commented Nov 28, 2014 at 18:52
  • possible duplicate of How to test if a string is JSON or not? – Zakaria Commented Nov 28, 2014 at 18:53
  • Ehm.... No. I don't want to know if a string IS a JSON or not but if a string IS IN a JSON... – pp94 Commented Nov 28, 2014 at 19:42
  • @SH.TheSuper you are right. Sorry for the duplicate. I put the question in the wrong way and I didn't notice that it was already there – pp94 Commented Nov 28, 2014 at 20:02
Add a ment  | 

2 Answers 2

Reset to default 3
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;
}

check if Json string has value in JS

Just try to parse it using JSON.parse, if the parse was successful return true else return false:

function isJSON(str) {
    try { 
      JSON.parse(str);
    } catch (e) {
      return false;
    }
    return true;
}
发布评论

评论列表(0)

  1. 暂无评论