最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - parsing json array in javascript with unknown field names - Stack Overflow

programmeradmin10浏览0评论

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

Share Improve this question edited Apr 13, 2013 at 13:52 G-J asked Apr 13, 2013 at 13:45 G-JG-J 1,0682 gold badges18 silver badges32 bronze badges 3
  • Do you have the possibility to change the returned data? usernames_count should really be a number and a new users 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 or Object.keys. In ES6 you have also for…of. – ZER0 Commented Apr 13, 2013 at 13:55
Add a ment  | 

3 Answers 3

Reset to default 5

You 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
    }
}
发布评论

评论列表(0)

  1. 暂无评论