简体   繁体   中英

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. my JS functions currently work to validate only one possible passcode. not sure how to approach scaling these functions out to iterate through the full JSON file. 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

{
   "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(var i in result)
{
    var count = result[i].REMAINING;
}

As it stands, you're accessing your JSON object incorrectly. 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 . It will make sense as passcode _ n is not adding any value to your codebase.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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