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 es 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
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 es 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
-
Do you have the possibility to change the returned data?
usernames_count
should really be a number and a newusers
field should be an array holding users. Don't you think? – Jonas G. Drange Commented Apr 13, 2013 at 13:49 - One thing that may be confusing you is that usernames_count is not an array, it is a JSON object. – Eric Levine Commented Apr 13, 2013 at 13:52
-
It's a JavaScript object, therefore you can iterate the keys – with a
for…in
orObject.keys
. In ES6 you have alsofor…of
. – ZER0 Commented Apr 13, 2013 at 13:55
3 Answers
Reset to default 5You can use Object.keys or a for…in loop – remember in that case to use hasOwnProperty:
var users = data.usernames_count;
Object.keys(users).forEach(function(user) {
console.log(user, users[user]);
});
Try this:
$.each(data.usernames_count, function(username, val) {
alert(username+" has a value of "+val);
});
It sounds like your question is how to iterate over the entries in the usernames_count
object. You can do that like so:
var key = '';
for(key in data.usernames_count) {
//check that this key isn't added from the prototype
if(data.usernames_count.hasOwnProperty(key) {
var value = data.usernames_count[key];
//do something with the key and value
}
}