简体   繁体   English

使用Javascript遍历JSON数据

[英]iterating through JSON data with Javascript

i'm working on a project where i have a series of passcodes that can only be used a predefined number of times. 我正在一个项目中,我有一系列只能预定义次数使用的密码。 right now, i have two JS functions validating and an external JSON file housing data to verify against. 现在,我有两个JS函数验证和一个外部JSON文件,其中包含要验证的数据。 my JS functions currently work to validate only one possible passcode. 我的JS函数目前只能验证一种可能的密码。 not sure how to approach scaling these functions out to iterate through the full JSON file. 不确定如何扩展这些功能以遍历整个JSON文件。 appreciate any direction. 欣赏任何方向。

thanks, 谢谢,

    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 JSON数据

{
   "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"
   }
}

To access an object you need loop through it via the for in loop. 要访问一个对象,您需要通过for in循环遍历该对象。

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

As it stands, you're accessing your JSON object incorrectly. 就目前而言,您正在错误地访问JSON对象。 Do this in your success callback: 在您的成功回调中执行以下操作:

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

I have an observation about your problem: 我对您的问题有看法:

You should have an array instead of a json . 您应该有一个array而不是json It will make sense as passcode _ n is not adding any value to your codebase. 因为passcode _ n不会在您的代码库中添加任何值,所以这是有道理的。

And here goes my soludion based on that observation: 根据这一观察,我得出结论:

Your array goes here: 您的数组在这里:

[
   "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"
   }
]

Here goes the way to iterate it : 这是迭代的方式:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM