I have found a lot of threads here on StackOverflow that talk about parsing json arrays but I can't seem to figure out how to get back some of the data. Here is what I have...
$('#keyword_form').submit(function(e){
var gj = $.post('employee_search.php',$('#keyword_form').serialize(),function(data){
if(!data || data.status !=1 )
{
alert(data.message);
return false;
}
else
{
alert(data.message);
}
},'json');
e.preventDefault();
});
The json data being sent to it looks like this...
{
"status":1,
"message":"Query executed in 9.946837 seconds.",
"usernames_count":{
"gjrowe":5,
"alisonrowe":4,
"bob":"1"
}
}
As my function shows I can do alert(data.message);
but how can I access the usernames_count
data?
My confusion comes from the fact that the data has no name/label. bob
is a username and 1
is the returned count associated with that username
If I do alert(usernames_count);
I get back [object Object]
If I do alert(usernames_count[0]);
I get back undefined
I am sure I should be doing something with JSON.parse();
but I haven't gotten it right yet
usernames_count
should really be a number and a newusers
field should be an array holding users. Don't you think? – Jonas G. Drange Apr 13 '13 at 13:49for…in
orObject.keys
. In ES6 you have alsofor…of
. – ZER0 Apr 13 '13 at 13:55