如何檢查JSON對象鍵是否具有某個值?如何檢查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輸出是這樣的:
[
{
"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"
}
]
你的方法是有效的(即使它是一個JS對象,而不是JSON),你是否有錯誤? – MinusFour
提供更好的問題解答。您沒有使用傳入該函數的參數,並且其他變量從無處出現 – charlietfl
我沒有得到任何回報。我更新了我的代碼。看上面。這是我如何提取 – Rorabih