使用Javascript迭代JSON数据

时间:2014-05-06 17:45:45

标签: javascript json

我正在开展一个项目,我有一系列只能使用预定义次数的密码。现在,我有两个JS函数验证和一个外部JSON文件,包含要验证的数据。我的JS函数目前只能验证一个可能的密码。不知道如何将这些函数扩展到迭代完整的JSON文件。欣赏任何方向。

感谢,

    function validateCode() {            
        var txtCode = document.getElementById("INPUT_PASSCODE");
        var results = txtCode.value.trim() != "" ? getRemainingCode(txtCode.value) : -1;

        if (results == 0) {
            alert('This code is no longer elegible');
            txtCode.value = '';
            txtCode.focus();

            return false;
        }
        else if (txtCode.value.trim() != "" && results == -1) {
            alert('Invalid code used');
            txtCode.value = '';
            txtCode.focus();

            return false;
        }

        return true;
    }

    function getRemainingCode(code) {
        var count = -1; //Not a valid code

        jQuery.ajax({
            url: '../codeCheck.aspx?Code=' + code + '&formhash=dfsgdfg',
            dataType: 'json',
            success: function (result) {
                count = result.REMAINING;

                if (isNaN(count))
                    count = -1;
            },

            async: false
        });

        return count;

    }

JSON DATA

{
   "Passcode_1":{
      "ID":"sdfg3456",
      "USED":"0",
      "REMAINING":"1",
      "TIMESTAMP":"4/30/2014 3:16:53 PM"
   },
   "Passcode_2":{
      "ID":"jkhl765",
      "USED":"0",
      "REMAINING":"1",
      "TIMESTAMP":"4/30/2014 3:16:53 PM"
   },
   "Passcode_3":{
      "ID":"cvbn435",
      "USED":"0",
      "REMAINING":"1",
      "TIMESTAMP":"4/30/2014 3:16:53 PM"
   },
   "Passcode_4":{
      "ID":"345fgh",
      "USED":"0",
      "REMAINING":"1",
      "TIMESTAMP":"4/30/2014 3:16:53 PM"
   },
   "Passcode_5":{
      "ID":"5hrdd54",
      "USED":"0",
      "REMAINING":"1",
      "TIMESTAMP":"4/30/2014 3:16:53 PM"
   }
}

3 个答案:

答案 0 :(得分:1)

要访问对象,您需要通过for in循环遍历它。

for(var i in result)
{
    var count = result[i].REMAINING;
}

答案 1 :(得分:0)

目前,您正在错误地访问JSON对象。在你的成功回调中这样做:

for(var i in result)
{
    var count = result[i].REMAINING;
}

答案 2 :(得分:0)

我对你的问题有一个观察:

您应该有一个array而不是json。这是有道理的,因为passcode _ n没有为您的代码库添加任何值。

根据这一观察,我的解决方案就在这里:

你的阵列在这里:

[
   "Passcode_1":{
      "ID":"sdfg3456",
      "USED":"0",
      "REMAINING":"1",
      "TIMESTAMP":"4/30/2014 3:16:53 PM"
   },
   "Passcode_2":{
      "ID":"jkhl765",
      "USED":"0",
      "REMAINING":"1",
      "TIMESTAMP":"4/30/2014 3:16:53 PM"
   },
   "Passcode_3":{
      "ID":"cvbn435",
      "USED":"0",
      "REMAINING":"1",
      "TIMESTAMP":"4/30/2014 3:16:53 PM"
   },
   "Passcode_4":{
      "ID":"345fgh",
      "USED":"0",
      "REMAINING":"1",
      "TIMESTAMP":"4/30/2014 3:16:53 PM"
   },
   "Passcode_5":{
      "ID":"5hrdd54",
      "USED":"0",
      "REMAINING":"1",
      "TIMESTAMP":"4/30/2014 3:16:53 PM"
   }
]

以下是迭代它的方法:

    (function(passcode){
      $.each(passcode_array, function(index, value){
        //validate passcode in function
        if(is_passcode_matched_and_valid(passcode, value)){
          //do things
        }
      });    
    })(passcode);