我需要过滤一些信息。该信息来自JSON解析。我不能使它工作。我想要的是将JSON过滤到HTML类中。我觉得我很傻
$.ajax({
// Agenda
type: 'POST',
url: 'agendas',
data: {results: 'events'},
dataType: 'json',
cache: false,
success: function (response) {
$('.date, .country, .events').html('');
$.each(response.results, function (index, result) {
if (result.status)
$('.date').append(result.server);
$('.country').append(result.server);
$('.events').append(result.server);
});
}
});
如果有人可以帮助我,那将很可爱
JSON:
{
"results": [
{
"events": {
"id": 1,
"date": "2022-05-06T00:00:00+00:00",
"description": "test",
"time": "2017-02-03T06:40:00+00:00",
"location": "NL",
"year": "2008",
"event": "Idk"
}
},
{
"events": {
"id": 2,
"date": "2019-04-05T00:00:00+00:00",
"description": "aasdasdasda",
"time": "2017-02-03T15:04:00+00:00",
"location": "asdasdasd",
"year": "0000",
"event": "asdasd"
}
}
]
}
HTML:
<div class="day">
<h2 class="date">Januari 23</h2>
<div class="country-events">
<span class="country">UK</span>
<div class="events">
<span class="event">Conference Amsterdam<br />11:00 CET</span>
<span class="event">Webinar Copenhagen<br />15:00 CET</span>
</div>
</div>
我真的无法解决这个问题。我知道这不是正确的代码,我是JSON和JavaScript的新手。目前是第4天,请原谅我。
先感谢您
更新
This is what it shows currently, the data from the json file needs to go in the specific elements
最佳答案
$.each(response.results, function(index, result) {
console.log(result)
$('.date').append(result.events.date);
$('.date').append('</br>');
$('.country').append(result.events.location);
$('.country').append('</br>');
$('.events').append(result.events.event);
$('.events').append('</br>');
});
这是你想要的吗?
https://plnkr.co/edit/wCYJPXgAPII1mcW4cKw7?p=preview
检查这个小提琴
关于javascript - JavaScript无法解析JSON,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42022589/