json 使用jQuery转换 AJAX 响应

oknrviil  于 2023-06-07  发布在  jQuery
关注(0)|答案(1)|浏览(212)

我有一个JSON,它是作为 AJAX 调用的响应来的

对象

[
   {
      "name":"Elly",
      "phone":"4345353534",
      "date":"2023-06-20",
      "time":"10:30:00",
      "comment":"Some comment here",
      "timestamp":"2023-06-06 09:16:28",
      "badge": "badge bg-success"
      "color": "green"
   },
   {
      "name":"Peter",
      "phone":"3943476534875",
      "date":"2023-06-20",
      "time":"18:00:00",
      "comment":"No comment",
      "timestamp":"2023-06-06 06:16:28",
      "badge": "badge bg-danger"
      "color": "red"
   }
]

** AJAX 调用**

$(document).on('click', '#bookings', function() {
    var account = $('#account').val();
    $.ajax({
        url:"bookings.php",
        method:"POST",
        data:{account:account},
        success:function(data) {
            //the JSON result
            //The function below is placed here
           }
    });
});

然后我有这个函数,我试图用 AJAX 响应中的数据创建events。在这个例子中, AJAX 只返回2条记录,但它们可以是任何数字。

函数

$(function (e) {
        var calendar = $("#calendar").calendarGC({
            dayBegin: 0,
            prevIcon: '<',
            nextIcon: '>',
            onPrevMonth: function (e) {
                console.log("prev");
                console.log(e)
            },
            onNextMonth: function (e) {
                console.log("next");
                console.log(e)
            },
            events: [
                {
                    date: new Date("2023-06-07"),
                    eventName: "Holiday",
                    className: "badge bg-danger",
                    onclick(e, data) {
                        console.log(data);
                    },
                    dateColor: "red"
                },
                {
                    date: new Date("2023-06-07"),
                    eventName: "Holiday with wife",
                    className: "badge bg-danger",
                    onclick(e, data) {
                        console.log(data);
                    },
                    dateColor: "red"
                }
            ],
            onclickDate: function (e, data) {
                console.log(e, data);
            }
        });
    })

因此,使用新的events函数可以变成这样

预期结果

$(function (e) {
        var calendar = $("#calendar").calendarGC({
            dayBegin: 0,
            prevIcon: '<',
            nextIcon: '>',
            onPrevMonth: function (e) {
                console.log("prev");
                console.log(e)
            },
            onNextMonth: function (e) {
                console.log("next");
                console.log(e)
            },
            events: [
                {
                    date: new Date("2023-06-20"),
                    eventName: "Elly",
                    className: "badge bg-success",
                    onclick(e, data) {
                        console.log(data);
                    },
                    dateColor: "green"
                },
                {
                    date: new Date("2023-06-20"),
                    eventName: "Peter",
                    className: "badge bg-danger",
                    onclick(e, data) {
                        console.log(data);
                    },
                    dateColor: "red"
                }
            ],
            onclickDate: function (e, data) {
                console.log(e, data);
            }
        });
    })

我所尝试的是

var json = $.parseJSON(data);
var array = [];
json.forEach((item) => {
    item = {}
    item ["date"] = new Date('"'+item.date+'"');
    item ["eventName"] = item.name;
    item ["className"] = item.badge;
    item ["dateColor"] = item.color;
    jsonObj.push(item);
});
jrcvhitl

jrcvhitl1#

1.每次都覆盖项
1.这样更简单

const data = `[ { "name":"Elly", "phone":"4345353534", "date":"2023-06-20", "time":"10:30:00", "comment":"Some comment here", "timestamp":"2023-06-06 09:16:28", "badge": "badge bg-success", "color": "green" }, { "name":"Peter", "phone":"3943476534875", "date":"2023-06-20", "time":"18:00:00", "comment":"No comment", "timestamp":"2023-06-06 06:16:28", "badge": "badge bg-danger", "color": "red" } ]`;

const obj = $.parseJSON(data);
const array = obj.map((item) => ({

  "date": new Date('"' + item.date + '"'),
  "eventName": item.name,
  "className": item.badge,
  "dateColor": item.color
}));

console.log(array)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

相关问题