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

时间:2014-10-27 13:07:49

标签: javascript jquery json

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

从该网站我使用的是inLine Json示例:

<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. 描述
  5. 那是&#39;事件&#39;硬编码版。

    现在我的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
             }
          ]
       }
    }
    

1 个答案:

答案 0 :(得分:1)

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

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

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),
相关问题