如何在ajax javascript中获取数据类型json的特定值键对?

时间:2014-11-11 09:42:34

标签: javascript ajax json

我希望获得" status"的价值。来自下面代码中的数据,

$.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); }
    });

以下是console.log结果的一部分:

  关键词:2014-11-11   价值:{&#34; 2014-11-11&#34;:[{&#34; 0&#34;:&#34; 3&#34;,&#34; 1&#34;:&#34; 2014 -11-11 11:11:00&#34;,&#34; 2&#34;:&#34; 2014-11-28 10:12:00&#34;,&#34; 3&#34;:& #34;测试&#34;&#34; 4&#34;:&#34;测试&#34;&#34; 5&#34;:&#34; 0&#34;&#34; 6&# 34;:&#34;&#34;&#34; 7&#34;:&#34;&#34;&#34; 8&#34;:&#34;&#34;&# 34; 9&#34;:&#34; 0&#34;&#34; 10&#34;:&#34; 0&#34;&#34; 11&#34;:&#34; 0&#34 ;,&#34; 12&#34;:&#34; 0&#34;&#34; 13&#34;:&#34; 0&#34;&#34; 14&#34;:&#34 ; 0&#34;&#34; 15&#34;:&#34; 0&#34;&#34; 16&#34;:&#34;&#34;&#34; 17&#34; :&#34; 2014年11月11日&#34;&#34; ID&#34;:&#34; 3&#34;&#34;时间戳&#34;:&#34; 2014年11月11日11:11:00&#34;,&#34; toTimestamp&#34;:&#34; 2014-11-28 10:12:00&#34;,&#34; title&#34;:&#34; test& #34;&#34;位置&#34;:&#34;测试&#34;&#34;状态&#34;:&#34; 0&#34;&#34;组织者&#34;:& #34;&#34;&#34; organizerContact&#34;:&#34;&#34;&#34; organizerEmail&#34;:&#34;&#34;&#34;投影仪&# 34;:&#34; 0&#34;&#34;膝上型&#34;:&#34; 0&#34;&#34;扬声器&#34;:&#34; 0&#34;&# 34;指针&#34;:&#34; 0&#34;&#34;白板&#34;:&#34; 0&#34;&# 34; mediaCoverage&#34;:&#34; 0&#34;&#34;停车&#34;:&#34; 0&#34;&#34;备注&#34;:&#34;&#34 ;,&#34;选择器&#34;:&#34; 2014年11月11日&#34;}],&#34; 2014年11月12日&#34;:[{&#34; 0&#34 ;: &#34; 15&#34;,&#34; 1&#34;:&#34; 2014-11-12 07:07:00&#34;,&#34; 2&#34;:&#34; 2014 -11-12 03:09:00&#34;,&#34; 3&#34;:&#34;测试&#34;,&#34; 4&#34;:&#34;测试&#34;,& #34; 5&#34;:&#34; 1&#34;&#34; 6&#34;:&#34;&#34;&#34; 7&#34;:&#34;&# 34;,&#34; 8&#34;:&#34;&#34;&#34; 9&#34;:&#34; 0&#34;&#34; 10&#34;:&# 34; 0&#34;&#34; 11&#34;:&#34; 0&#34;&#34; 12&#34;:&#34; 0&#34;&#34; 13&#34 ;:&#34; 0&#34;&#34; 14&#34;:&#34; 0&#34;&#34; 15&#34;:&#34; 0&#34;&#34 ; 16&#34;:&#34;&#34;&#34; 17&#34;:&#34;二〇一四年十一月一十二日&#34;&#34; ID&#34;:&#34; 15&#34;,&#34; timestamp&#34;:&#34; 2014-11-12 07:07:00&#34;,&#34; toTimestamp&#34;:&#34; 2014-11-12 03:09:00&#34;&#34;标题&#34;:&#34;测试&#34;&#34;位置&#34;:&#34;测试&#34;&#34;状态& #34;:&#34; 1&#34;&#34;组织者&#34;:&#34;&#34;&#34; organizerContact&#34;:&#34;&#34;,& #34; organizerEmail&#34;:&#34;&#34;&#34;投影仪&#34;:&#34; 0&# 34;,&#34;膝上型&#34;:&#34; 0&#34;&#34;扬声器&#34;:&#34; 0&#34;&#34;指针&#34;:&# 34; 0&#34;&#34;白板&#34;:&#34; 0&#34;&#34; mediaCoverage&#34;:&#34; 0&#34;&#34;停车&#34 ;:&#34; 0&#34;&#34;备注&#34;:&#34;&#34;&#34;选择器&#34;:&#34; 2014年11月12日&#34; }]}

我想获得&#34; status&#34;的价值即0,如上面的结果所示,为了将它包含在for循环中(对于(数据中的var键){...})来改变类&#39;标签 - 成功&#39;标签失败&#39;如果状态为0.你能帮助我吗?

3 个答案:

答案 0 :(得分:1)

你好,也许我错了,但是

console.log('key: ' + key + '\n' + 'value: ' + JSON.stringify(data));
当你说JSON.stringify(data)时,

以字符串格式返回整个数据对象; 您需要在为数据提供特定键以从以下位置读取值时返回的值:

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

编辑:我不确定data [key]是否会返回[object Object] ...如果它确实尝试了JSON.stringify(data [key])

我还建议使用

通过数据进行迭代
for(var i = 0; i < data.length; i++){} 

这使得它具有可读性,并且是以最高效的方式提取数据的。

编辑nr 2:

这是你的对象:

{"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"}]}

这有点嵌套,所以试着概述一下你的内容:

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

此对象有一个length方法,用于返回对象的长度。这可以让你对数据对象进行迭代会给你&#34; 2014-11-11&#34;作为键,使用此键可以访问您的值,如下所示:data [key]这将返回您的数组...要从数组中读取数据,您将不得不再次使用数据[key] [i] ...现在你可以像这样读取每个数组元素中的数据

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

希望这有所帮助...不能写下所有这些代码:D

答案 1 :(得分:0)

 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));
            }
        },

答案 2 :(得分:0)

请尝试使用此代码。

$.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); }
});