Javed Ahmed
Javed Ahmed

Reputation: 406

How do I get specific value key pair in ajax javascript for datatype json?

I want to get the value of "status" from data in the code below,

$.ajax({
        dataType: "json",
        url: "calendar/php/date.php",
        type: "POST",
        data: {
            select: "%Y-%c-%e",
            where: "%Y-%c",
            d: y + "-" + m,
            order: "timestamp, id"
        },
        beforeSend: function() { $('#loading').show(); },
        success: function(data) {
            sessionStorage[y+"-"+m] = JSON.stringify(data);
            for (var key in data) {
                $("<span class=\"label label-success\">" + Object.keys(data[key]).length + "</span>").prependTo($("#" + key));
                console.log('key: ' + key + '\n' + 'value: ' + JSON.stringify(data));
            }
        },
        complete: function() { $('#loading').fadeOut(200); }
    });

Following is some part of the console.log result:

key: 2014-11-11 value: {"2014-11-11":[{"0":"3","1":"2014-11-11 11:11:00","2":"2014-11-28 10:12:00","3":"test","4":"test","5":"0","6":"","7":"","8":"","9":"0","10":"0","11":"0","12":"0","13":"0","14":"0","15":"0","16":"","17":"2014-11-11","id":"3","timestamp":"2014-11-11 11:11:00","toTimestamp":"2014-11-28 10:12:00","title":"test","location":"test","status":"0","organizer":"","organizerContact":"","organizerEmail":"","projector":"0","laptop":"0","speaker":"0","pointer":"0","whiteboard":"0","mediaCoverage":"0","parking":"0","remark":"","selector":"2014-11-11"}],"2014-11-12":[{"0":"15","1":"2014-11-12 07:07:00","2":"2014-11-12 03:09:00","3":"test","4":"test","5":"1","6":"","7":"","8":"","9":"0","10":"0","11":"0","12":"0","13":"0","14":"0","15":"0","16":"","17":"2014-11-12","id":"15","timestamp":"2014-11-12 07:07:00","toTimestamp":"2014-11-12 03:09:00","title":"test","location":"test","status":"1","organizer":"","organizerContact":"","organizerEmail":"","projector":"0","laptop":"0","speaker":"0","pointer":"0","whiteboard":"0","mediaCoverage":"0","parking":"0","remark":"","selector":"2014-11-12"}]}

I want get the value of "status" i.e 0, as seen in the above result, in order to include it in the for loop (for (var key in data) {...}) to change the class 'label-success' to 'label-failure' if the status is 0. Could you help me?

Upvotes: 0

Views: 1607

Answers (3)

Alvin Magalona
Alvin Magalona

Reputation: 771

Try this code instead.

$.ajax({
    dataType: "json",
    url: "calendar/php/date.php",
    type: "POST",
    data: {
        select: "%Y-%c-%e",
        where: "%Y-%c",
        d: y + "-" + m,
        order: "timestamp, id"
    },
    beforeSend: function() { $('#loading').show(); },
    success: function(data) {
        sessionStorage[y+"-"+m] = JSON.stringify(data);
        for (var key in data) {
            for (var i in data[key]) {
                $("<span class=\"label " + ((data[key][i] === "0") ? "label-failure" : "label-success") + "\">" + Object.keys(data[key]).length + "</span>").prependTo($("#" + key));
            }
            console.log('key: ' + key + '\n' + 'value: ' + JSON.stringify(data));
        }
    },
    complete: function() { $('#loading').fadeOut(200); }
});

Upvotes: 0

Max Bumaye
Max Bumaye

Reputation: 1009

Hello maybe I am wrong but

console.log('key: ' + key + '\n' + 'value: ' + JSON.stringify(data));

returns the whole data object in string format when you say JSON.stringify(data); You want the value which is returned when you give data a specific key to read values from:

console.log('key: ' + key + '\n' + 'value: ' + data[key]);

EDIT: Im not sure if data[key] will return a [object Object]... if it does try JSON.stringify(data[key])

I would also suggest Itterating through data with the

for(var i = 0; i < data.length; i++){} 

this makes it readable and is measured the most performant way of extracting data.

EDIT nr 2:

This is your object:

{"2014-11-11":[{"0":"3","1":"2014-11-11 11:11:00","2":"2014-11-28 10:12:00","3":"test","4":"test","5":"0","6":"","7":"","8":"","9":"0","10":"0","11":"0","12":"0","13":"0","14":"0","15":"0","16":"","17":"2014-11-11","id":"3","timestamp":"2014-11-11 11:11:00","toTimestamp":"2014-11-28 10:12:00","title":"test","location":"test","status":"0","organizer":"","organizerContact":"","organizerEmail":"","projector":"0","laptop":"0","speaker":"0","pointer":"0","whiteboard":"0","mediaCoverage":"0","parking":"0","remark":"","selector":"2014-11-11"}],"2014-11-12":[{"0":"15","1":"2014-11-12 07:07:00","2":"2014-11-12 03:09:00","3":"test","4":"test","5":"1","6":"","7":"","8":"","9":"0","10":"0","11":"0","12":"0","13":"0","14":"0","15":"0","16":"","17":"2014-11-12","id":"15","timestamp":"2014-11-12 07:07:00","toTimestamp":"2014-11-12 03:09:00","title":"test","location":"test","status":"1","organizer":"","organizerContact":"","organizerEmail":"","projector":"0","laptop":"0","speaker":"0","pointer":"0","whiteboard":"0","mediaCoverage":"0","parking":"0","remark":"","selector":"2014-11-12"}]}

this is a bit nested, so try to get an overview of what you have:

data = { 
  "2014-11-11": [],
  "2014-11-12": []... }

This object has a length method that returns the length of your object. this allows you to itterate over the Data Object will give you "2014-11-11" as key and with this key you can access your values like this: data[key] this will return your array...to read data from your array you will have to itterate again data[key][i]... now you can read the data inside each array element like this

data[key][i]["status"];

Hope this helped somehow... cant be bothered to write all this code :D

Upvotes: 1

Sean
Sean

Reputation: 3042

 success: function(data) {
            sessionStorage[y+"-"+m] = JSON.stringify(data);
            for (var key in data) {
                var status = data['status'];
                var klass = status === 0 ? 'label-failure' : 'label-success';
                $('<span class="label '+klass+'">' + Object.keys(data[key]).length + "</span>").prependTo($("#" + key));
                console.log('key: ' + key + '\n' + 'value: ' + JSON.stringify(data));
            }
        },

Upvotes: 0

Related Questions