如何将一个JSON对象转换为另一个JSON object

How to convert one JSON object to another JSON Object

本文关键字:JSON 对象 转换 object 另一个 一个      更新时间:2023-09-26

我正在尝试使用下面的插件来呈现我们的sharepoint事件列表中的一些事件数据。http://www.vissit.com/projects/eventCalendar/

从那个网站,我正在使用inLineJson示例如下:

<div id="eventCalendarInline"></div>
        <script>
            $(document).ready(function () {
                var eventsInline = [{ "date": "1414490400000", "type": "meeting", "title": "Project A meeting", "description": "Lorem Ipsum dolor set", "url": "http://www.event1.com/" },
                    { "date": "1414490400000", "type": "demo", "title": "Project B demo", "description": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", "url": "http://www.event2.com/" },
                    { "date": "1414490400000", "type": "meeting", "title": "Project A meeting", "description": "Lorem Ipsum dolor set", "url": "http://www.event1.com/" },
                    { "date": "1338885237000", "type": "demo", "title": "Project B demo", "description": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", "url": "http://www.event2.com/" },
                    { "date": "1414490400000", "type": "meeting", "title": "Project A meeting", "description": "Lorem Ipsum dolor set", "url": "http://www.event1.com/" },
                    { "date": "1414490400000", "type": "demo", "title": "Project B demo", "description": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", "url": "http://www.event2.com/" },
                    { "date": "1344515447000", "type": "meeting", "title": "Project A meeting", "description": "Lorem Ipsum dolor set", "url": "http://www.event1.com/" },
                    { "date": "1345033847000", "type": "demo", "title": "Project B demo", "description": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", "url": "http://www.event2.com/" },
                    { "date": "1347712247000", "type": "meeting", "title": "Project A meeting", "description": "Lorem Ipsum dolor set", "url": "http://www.event1.com/" },
                    { "date": "1348230647000", "type": "demo", "title": "Project B demo", "description": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", "url": "http://www.event2.com/" },
                    { "date": "1349094647000", "type": "meeting", "title": "Project A meeting", "description": "Lorem Ipsum dolor set", "url": "http://www.event1.com/" },
                    { "date": "1351600247", "type": "demo", "title": "Project B demo", "description": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", "url": "http://www.event2.com/" }];
                $("#eventCalendarInline").eventCalendar({
                    jsonData: eventsInline,
                    openEventInNewWindow: true,
                    showDescription: true,
                    eventsScrollable: true
                });
            });
        </script>

如您所见,共有4个字段:

  1. 日期
  2. 类型
  3. 标题
  4. 说明

那是"事件"的硬编码版本。

现在,我的JS代码已经用JSON返回了项目,我需要将其转换为与上面相同的格式,并且日期也应该转换为该格式。

var SPHostUrl;
var SPAppWebUrl;
var ready = false;
// this function is executed when the page has finished loading. It performs two tasks:
//    1. It extracts the parameters from the url
//    2. It loads the request executor script from the host web
$(document).ready(function () {
    var params = document.URL.split("?")[1].split("&");
    for (var i = 0; i < params.length; i = i + 1) {
        var param = params[i].split("=");
        switch (param[0]) {
            case "SPAppWebUrl":
                SPAppWebUrl = decodeURIComponent(param[1]);
                break;
            case "SPHostUrl":
                SPHostUrl = decodeURIComponent(param[1]);
                break;
        }
    }
    // load the executor script, once completed set the ready variable to true so that
    // we can easily identify if the script has been loaded
    $.getScript(SPHostUrl + "/_Layouts/15/SP.RequestExecutor.js", function (data) {
        ready = true;
        getItems();
    });
});
// this function retrieves the items within a list which is contained within the parent web
function getItems() {
    // only execute this function if the script has been loaded
    if (ready) {
        // the name of the list to interact with
        var listName = "Events";
        // the url to use for the REST call.
        var url = SPAppWebUrl + "/_api/SP.AppContextSite(@target)" +
            // this is the location of the item in the parent web. This is the line
            // you would need to change to add filters, query the site etc
            "/web/lists/getbytitle('" + listName + "')/items?$select=Title,Category,EventDate,Description" +
            "&@target='" + SPHostUrl + "'";
        // create  new executor passing it the url created previously
        var executor = new SP.RequestExecutor(SPAppWebUrl);
        // execute the request, this is similar although not the same as a standard AJAX request
        executor.executeAsync(
            {
                url: url,
                method: "GET",
                headers: { "Accept": "application/json; odata=verbose" },
                success: function (data) {
                    // parse the results into an object that you can use within javascript
                    var results = eval(JSON.parse(data.body));
                },
                error: function (data) {
                    // an error occured, the details can be found in the data object.
                    alert("Ooops an error occured");
                }
            });
    }
}

JSON返回的屏幕截图如下:

http://screencast.com/t/KF6tBMDYrjS

来自sharepoint 的JSON

{
   "d":{
      "results":[
         {
            "__metadata":{
               "id":"Web/Lists(guid'f33a5776-235c-4187-a0de-4db747221854')/Items(1)",
               "uri":"http://apps-f5dd09fb663079.apps.com/MiniCalendar/_api/Web/Lists(guid'f33a5776-235c-4187-a0de-4db747221854')/Items(1)",
               "etag":"'"1'"",
               "type":"SP.Data.EventsListItem"
            },
            "Title":"GTS Meeting",
            "EventDate":"2014-10-14T15:00:00Z",
            "Description":"&lt;div&gt;&lt;/div&gt;",
            "Category":"Meeting"
         },
         {
            "__metadata":{
               "id":"Web/Lists(guid'f33a5776-235c-4187-a0de-4db747221854')/Items(2)",
               "uri":"http://apps-f5dd09fb663079.apps.com/MiniCalendar/_api/Web/Lists(guid'f33a5776-235c-4187-a0de-4db747221854')/Items(2)",
               "etag":"'"1'"",
               "type":"SP.Data.EventsListItem"
            },
            "Title":"Event 2",
            "EventDate":"2014-10-21T10:00:00Z",
            "Description":null,
            "Category":null
         },
         {
            "__metadata":{
               "id":"Web/Lists(guid'f33a5776-235c-4187-a0de-4db747221854')/Items(3)",
               "uri":"http://apps-f5dd09fb663079.apps.com/MiniCalendar/_api/Web/Lists(guid'f33a5776-235c-4187-a0de-4db747221854')/Items(3)",
               "etag":"'"1'"",
               "type":"SP.Data.EventsListItem"
            },
            "Title":"Event3",
            "EventDate":"2014-10-29T09:00:00Z",
            "Description":"  dsa dsa dsa dsa dsa dsa dsa dsa dsa dsa",
            "Category":null
         },
         {
            "__metadata":{
               "id":"Web/Lists(guid'f33a5776-235c-4187-a0de-4db747221854')/Items(4)",
               "uri":"http://apps-f5dd09fb663079.apps.com/MiniCalendar/_api/Web/Lists(guid'f33a5776-235c-4187-a0de-4db747221854')/Items(4)",
               "etag":"'"1'"",
               "type":"SP.Data.EventsListItem"
            },
            "Title":"sad sadsa dsa das",
            "EventDate":"2014-10-22T09:00:00Z",
            "Description":" asdsa dsa dsa dsa dsa dsa dsa dsa",
            "Category":null
         }
      ]
   }
}

没有自动转换这两个对象的方法。你需要自己写代码:

假设您知道如何在变量e中的result.d.results中迭代数组,则可以创建如下事件:

var event = {
    type: e.Category.toLowercase(), 
    title: e.Title,
    description: e.Description
};

不过,从2014-10-21T10:00:00Z到数字的转换更为复杂。输入格式看起来有点像ISO-8601加时区(其中Z==UTC)。对于这些,Date.parse()应该起作用:

    date: Date.parse(e.EventDate),