如何将一个JSON对象转换为另一个JSON object
How to convert one JSON object to another JSON Object
我正在尝试使用下面的插件来呈现我们的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个字段:
- 日期
- 类型
- 标题
- 说明
那是"事件"的硬编码版本。
现在,我的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":"<div></div>",
"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),
相关文章:
- jQuery匹配JSON对象的部分文本
- 如何在Javascript中将JSon对象转换为数组
- 我可以在json对象中添加一个函数吗
- 使用JS将数组转换为json对象
- 如何为json对象中的段发送array[]
- 将JSON对象传递给angular指令
- 更改JSON对象的结构
- 访问JSON对象内部的数组元素
- 在ejs-partial中对JSON对象进行迭代
- 遍历 JSON 对象并检查 URL 是否以某个值结尾
- 访问嵌套JSON对象的键,其中键是动态的
- json对象中缺少对象循环
- 发送json对象或使用express路由呈现视图
- 在play2框架中向json对象添加下拉列表项
- 元素名称上带有短划线 (-) 字符的 Json 对象
- autocomplete不接受源的json对象
- 如何在javascript中创建动态json对象
- 在使用客户端脚本时拾取JSON对象
- 如何通过json对象数组为嵌套对象赋值
- 构造JSON对象