www.webdeveloper.com
Results 1 to 4 of 4

Thread: accessing arrays within JSON

  1. #1
    Join Date
    Feb 2005
    Posts
    22

    accessing arrays within JSON

    Hello all,
    So far I have:

    code:
    Code:
    			$("#submit").click(function(){
    
    				var validate = 
    				{
    					"validateRules":
    					[
            					{
    							"fieldId": "registration",
    							"errMsg": errNumeric,
    							"errDiv": "#regmsg",
    							"pattern": regexNumeric
    						},
    						{
    							"errDiv": "#regmsg"
    						},
    						{
    							"errDiv": "#speciesmsg"
    						},
    						{
    							"errDiv": "#gendermsg"
    						},
    						{
    							"errDiv": "#bdaymsg"
    						},
    						{
    							"errDiv": "#aliasmsg"
    						}
        					]
    
    				};
    
    				alert(validate.ValidateRules[0].errDiv);
    	
    			});
    I can access arrays within that JSON code by:

    Code:
    validate.ValidateRules[0].errDiv
    How do I loop through validate and if errDiv is found grab it's value and do something....

    -Rich

  2. #2
    Join Date
    Aug 2007
    Posts
    3,767
    Using a loop.
    Code:
    for (var i = 0, v = validate.validateRules, l = v.length; i < l; i++) {
    if (v[i].errDiv) alert("");
    }

  3. #3
    Join Date
    Jul 2005
    Location
    USA
    Posts
    912
    Code:
    for (i=0;i<validate.validateRules.length;i++)
    {
       var obj = validate.validateRules[i];
       if(obj.errDiv)
       {
          // do something
       }
    }

  4. #4
    Join Date
    Feb 2005
    Posts
    22
    Thanks Declan1991 and webnerb. Both worked as expected :-).

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles