我有一个JSON url文件,我从中抓取了数据并写到了li。 JSON文件的链接是https://www.inquicker.com/facility/americas-family-doctors.json。我的小提琴文件的链接是http://jsfiddle.net/VVnSC/17/。我正在尝试将时间缩小到每个位置的下一个可用时间。因此,每个地点布伦特伍德,士麦那和斯普林希尔只有一次(下一个可用时间)。我也试图在名称中的破折号之前删除位置名称,以便它只显示该人的姓名。非常感谢所有帮助,谢谢。
<!DOCTYPE html>
<html>
<head>
<title>AFD TEST</title>
<script type="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"> </script>
<script>
$(document).ready(function(){
$.getJSON('https://www.inquicker.com/facility/americas-family-doctors.json',
function(data){
console.log(data.facility);
$.each(data.schedules, function(i, name){
times=''
if (name.available_times.length){
times='<ul>'
$.each(name.available_times,function(i,o){
for (var i; i < 1; i++) {
times+='<li><a href="'+ name.available_times[0].url +'">'+o.when+'</a></li>'
}
})
times+='</ul>'
}
else{
times='<ul><li>No Time Available</li></ul>'
}
$('#names').append('<li>' + (name.name) + times + '</li>');
});
});
});
</script>
</head>
<body>
<ul id="names"></ul>
</body>
</html>
答案 0 :(得分:0)
你应该使用正则表达式。像
这样的东西var regexp = /- (.*)/g;
var nameShort = regexp.exec(name.name);
该名称将位于nameShort [1]
中所以在你的代码中替换
$('#names').append('<li>' + (name.name) + times + '</li>');
与
var regexp = /- (.*)/g;
var nameShort = regexp.exec(name.name);
$('#names').append('<li>' + (nameShort[1]) + times + '</li>');
答案 1 :(得分:0)
如果有模式
,使用regexp是更好的选择只需更改此代码段
即可$('#names').append('<li>' + (name.name) + times + '</li>');
用这个
$('#names').append('<li>' + (name.name.replace(/.*?-\s/,'')) + times + '</li>');
试试这个 我在这里有更新
http://jsfiddle.net/VVnSC/47/
答案 2 :(得分:0)
你必须做一些花哨的约会&amp;时间比较,解析和比较日期和时间。
以下解析时间和日期(请参阅this fiddle了解其工作原理)。这也需要date.js
(它作为小提琴中的资源添加):
$(document).ready(function(){
$.getJSON('https://www.inquicker.com/facility/americas-family-doctors.json',
function(data){
var earliest = {};
var doctor = {};
var links = {};
$.each(data.schedules, function(i, name) {
var location = name.name.split(' - ')[0];
var dr_name = name.name.split(' - ')[1];
if (name.available_times.length) {
if (location in earliest) { // location has already been stored.
var newTime = parseAvailableDate(name.available_times[0].when);
if (newTime.isBefore(earliest[location])) {
earliest[location] = newTime;
doctor[location] = dr_name;
links[location] = name.available_times[0].url;
}
}
else {
earliest[location] = parseAvailableDate(name.available_times[0].when);
doctor[location] = dr_name;
links[location] = name.available_times[0].url;
}
}
});
for (i in earliest) {
$("#names").append("<ul>Location: "+i+"<li>Doctor: "+doctor[i]+"</li><li>Time: <a href='"+links[i]+"'>"+earliest[i].toString("dddd dd MMMM yyyy H:mm tt")+"</a></li></ul>");
}
});
});
function parseAvailableDate(dateString) {
var trimmedString = dateString.replace(/^\s\s*/, '');
var avTime=trimmedString.split(' ')[0],
ampm=trimmedString.split(' ')[1],
avDay=trimmedString.split(' ')[2];
var avDate = Date.parse("next "+avDay);
avDate.addHours(avTime.split(':')[0]).addMinutes(avTime.split(':')[1]);
if (ampm == "pm" && avTime.split(':')[0] != "12") avDate.addHours(12);
return avDate;
}
修改强>
如果位置已修复且您有订单,请更改此选项:
for (i in earliest) {
$("#names").append("<ul>Location: "+i+"<li>Doctor: "+doctor[i]+"</li><li>Time: <a href='"+links[i]+"'>"+earliest[i].toString("dddd dd MMMM yyyy H:mm tt")+"</a></li></ul>");
}
为:
displayDetails("Brentwood", earliest, doctor, links);
displayDetails("Smyrna", earliest, doctor, links);
displayDetails("Spring Hill", earliest, doctor, links);
并创建此函数(see another fiddle):
function displayDetails(location, earliest, doctor, links) {
$("#names").append("<ul>Location: "+location+"<li>Doctor: "+doctor[location]+"</li><li>Time: <a href='"+links[location]+"'>"+earliest[location].toString("dddd dd MMMM yyyy H:mm tt")+"</a></li></ul>");
}