【问题标题】:JavaScript failing parsing JSONJavaScript 无法解析 JSON
【发布时间】:2017-06-20 17:26:52
【问题描述】:

我需要过滤一些信息。该信息来自 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

【问题讨论】:

  • 与其说“我不能让它工作”,不如试着准确解释一下你发布的代码什么出了问题.它会出错吗?它会产生意想不到的输出吗?如果人们知道您的问题是什么,他们将更有能力提供帮助。
  • 您的result 对象没有status 属性。
  • @evolutionxbox 或 server.
  • 这就是问题所在,它没有错误,也没有意外的输出。我正在努力为我的问题找到解决方案,但遗憾的是,这对我来说并不是愉快的一天。
  • @DamianAshworth 您正在寻找什么解决方案?

标签: javascript jquery json parsing


【解决方案1】:
$.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

检查这个小提琴

【讨论】:

  • 我试过了,它仍然没有在我的仪表板上显示任何内容
  • response.results.date 未定义请参阅他提供的 JSON 文件!
  • 试试小提琴,这基本上就是你成功的方法
  • @evolutionxbox 抱歉,您能明确说明什么不起作用吗?
  • 根据 OP 发布的 JSON,response.results.date 将是 undefined
猜你喜欢
  • 1970-01-01
  • 2021-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多