Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 144
  • Last Modified:

Correct javascript syntax to extract jSON array data?

Experts,

I'm using jQuery's $.getJSON() function to pull data from my MySQL db.

I am able to return a proper JSON array via my PHP file but, I can't figure out how to call the data stored in the array?

I'm attempting to place the first and last name found within the jSON array into an input "$('#'+update_value)".

The values being placed into the input show as "undefined undefined".

jQuery Function
//////////////////////////////////////
//FIND EACH SPONSOR OR ALT USER NAME
$.each($(".alt_user, .sponsor"), function(){
	var NUID = $(this).val();
	var update_value = $(this).attr('update_value');
	if(NUID != ''){
		$.getJSON(
			"scripts/find_nuid.php", 
			{
				term:NUID
			}, 
			function(result){
				$('#'+update_value).val(result.first_name+' '+result.last_name);
			}//END FUNCTION
		);
	}//END IF
});

Open in new window


Example JSON array produced from the PHP file:
[{"first_name":"Joe","last_name":"Blow","nuid":"X123456"}]

Open in new window

0
evibesmusic
Asked:
evibesmusic
1 Solution
 
Kim WalkerWeb Programmer/TechnicianCommented:
JSON is an object, not an array. Your example shows a JSON object embedded in an array. The JSON object is...
{"first_name":"Joe","last_name":"Blow","nuid":"X123456"}

Open in new window

The square brackets on each end are unnecessary. You must remove those square brackets at each end of the returned value in your PHP file for your existing jQuery code to work. Otherwise I would expect the getJSON call to fail because the result is not a properly formatted JSON object.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now