如何检查JSON对象键是否具有特定值?
我使用PHP获取数据,使用json_encode获取JSON中的输出。之后我使用Ajax将结果附加到div。
我想要实现的是,如果某个键具有某个值 - 然后将它们组合在一起:这样的事情:
If Key_Status = "value_Solved" {
// do something
} else If Key_Status = "value_Not_Solved" { {
// do something else
};
我没有收到任何错误或输出。
这是我迄今取得的成就:
$.ajax({
url: "api-json.php?api",
data: {id: idGet},
dataType: "text",
success: function(data) {
var json = $.parseJSON(data);
if (json) {
$.each(json, function(idx, obj) {
//now json variable contains data in obj format
//let's display a few items
var id = obj.task.id;
var customer = obj.task.customer;
var task = obj.task.task;
var status = function(key, val) {
var elm = obj.task.status;
if ( elm == 'Solved' ) {
$("#"+id).css("background", "blue");
console.log("#"+id);
}
};
var solver = obj.task.solver;
var issuer = obj.task.issuer;
var added = obj.task.added;
var label = obj.task.label;
$('#results')
.append(" <div class='board-list-box' id='" + id + "'> <a class='board-list-box-edit'><span class='edit'></span><span class='update'></span><span class='delete'></span><span class='screen-reader-text'>Edit</span></a> <label class='board-list-box-label " + label + "'></label> <p class='board-list-box-text'> " + task + "</p> <span class='board-list-box-info'>" + added + "</span> </div>");
});
} else {
$('#results').html("<p class='error'>" + json + " tasks to do </p>");
}
}
});
我的JSON OUTPUT是这样的:
[
{
"task": {
"id": "537",
"task": "Test 1",
"description": "",
"customer": "",
"issuer": "",
"solver": "",
"label": "green",
"status": "Not started",
"deadline": "0000-00-00 00:00:00",
"added": "16-01-24 06:20:55"
},
"comments": "comments",
"labels": "labels"
},
{
"task": {
"id": "538",
"task": "Test 2",
"description": "",
"customer": "",
"issuer": "",
"solver": "",
"label": "green",
"status": "Not started",
"deadline": "0000-00-00 00:00:00",
"added": "16-01-24 06:20:58"
},
"comments": "comments",
"labels": "labels"
}
答案 0 :(得分:1)
编辑答案:
让我们参考以下方法:
-Xmx2048m
function CheckTasks() {
var Tasks = '[{ "task": { "id": "537", "task": "Test 1", "description": "", "customer": "", "issuer": "", "solver": "", "label": "green", "status": "Not started", "deadline": "0000-00-00 00:00:00", "added": "16-01-24 06:20:55" }, "comments": "comments", "labels": "labels" }, { "task": { "id": "538", "task": "Test 2", "description": "", "customer": "", "issuer": "", "solver": "", "label": "green", "status": "Not started", "deadline": "0000-00-00 00:00:00", "added": "16-01-24 06:20:58" }, "comments": "comments", "labels": "labels" }]';
var obj = JSON.parse(Tasks);
var groupedTasks=[]; // Array which will have the Task IDs if the Status is Not started.
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
//alert(obj[key].task.status); // To access the Status of a Task
if (obj[key].task.status == "Not started") {
groupedTasks.push(obj[key].task.id); // Push the Task ID in the array.
}
}
}
alert(groupedTasks) ; // The Grouped Tasks with Status as Not started will be available in this Array.
}
方法将字符串解析为JSON。
JSON.parse()
指的是存在的任务数量。我们有2个任务(537&amp; 538)。
key
方法返回一个布尔值,指示对象是否具有指定的hasOwnProperty()
。
在上面的代码中,我们遍历每个property
(任务)并检查它是否有key
。如果它有property
,请使用property
获取value
。
创建数组obj[key].task.status
,如果状态为未启动,则将这些任务的ID添加(推送)到此数组中。
请参阅此JSFiddle链接并查看结果。