我们如何格式化我们的API Json,就像下面的第一个代码块一样?我们需要将它放在数组,字符串,对象中吗?我们的问题是,完整日历可以从第一个代码块中获取事件,但不能从我们的第二个代码块中获取事件。
http://www.json-generator.com/api/json/get/ccUKVDYErS?indent=2
[
{
"start": "2014-08-01",
"title": "All Day Event"
},
{
"start": "2014-01-07",
"end": "2014-08-10",
"title": "Long Event"
},
{
"start": "2014-08-09T16:00:00",
"id": 999,
"title": "Repeating Event"
},
{
"start": "2014-08-16T16:00:00",
"id": 999,
"title": "Repeating Event"
},
{
"start": "2014-08-12T10:30:00",
"end": "2014-08-12T12:30:00",
"title": "Meeting"
},
{
"start": "2014-08-22T12:00:00",
"title": "Lunch"
},
{
"start": "2014-08-13T07:00:00",
"title": "Birthday Party"
},
{
"url": "http://google.com/",
"start": "2014-08-28",
"title": "Click for Google"
}
]
我们的API目前看起来像这样(一切都是字符串),
[{"start":"2016-04-12","end":"2016-04-12","title":"Calendar 1","id":"a41380d1fbbaa819"}]
答案 0 :(得分:1)
试试这个会起作用:
根据你的问题。
//JSON Text
var obj = '[{"start":"2016-04-12","end":"2016-04-12","title":"Calendar 1","id":"a41380d1fbbaa819"}]';
//Convert JSON String in JSON Object
console.log(JSON.parse(obj));
输出
根据您的要求:
您必须在url中传递这些值并进行AJAX调用以根据这些值获取JSON。
var start='2016-04-12';
var end='2016-04-12';
var title='Calendar 1';
var id='var start='2016-04-12';';
$.ajax({
type: "POST",
dataType: "json",
url: "data.php", //Relative or absolute path to chat.php file
data: 'start='+start+'&end='+end+'&title='+title+'&id='+id,
success: function(data) {
alert(data); // JSON From the URL
}
});
return false;
}
现在您必须使用服务器端语言(PHP,Java)根据这些值创建JSON。
答案 1 :(得分:0)
谈论你的API,你说的是#(所有内容都是字符串)'。
要将字符串转换为JSON,请使用JSON.parse()
docs
var validJSON = JSON.parse('[{"start":"2016-04-12","end":"2016-04-12","title":"Calendar 1","id":"a41380d1fbbaa819"}]');
将返回:
[{
"start": "2016-04-12",
"end": "2016-04-12",
"title": "Calendar 1",
"id": "a41380d1fbbaa819"
}]