JavaScript 将字符串拆分为 json

发布于 2025-01-18 04:55:46 字数 478 浏览 0 评论 0原文

我想将字符串拆分为 JSON。下面是一些例子:

Mon, Fri 2:30 pm - 8 pm / Tues 15 am - 2 pm / Weds 1:15 pm - 3:15 am /....

Mon, Weds - Thurs, Sat 7:15 pm - 3:30 am / Tues 4:45 pm - 5 pm / Fri 8:25 am - 9:30 pm / ...

Mon, Weds - Sun 7:15 pm - 3:30 am

Mon - Sun 7:15 pm - 3:30 am

我希望我能从每一行获取 JSON:

[
    {
        day: Mon,
        openning_time: 7:15 pm
        closing_time: 3:30 am
    },
    ....
]

我已经尝试了很多方法但仍然无法做到。希望能得到一些想法

I would like to split the string into JSON. Below is some example:

Mon, Fri 2:30 pm - 8 pm / Tues 15 am - 2 pm / Weds 1:15 pm - 3:15 am /....

Mon, Weds - Thurs, Sat 7:15 pm - 3:30 am / Tues 4:45 pm - 5 pm / Fri 8:25 am - 9:30 pm / ...

Mon, Weds - Sun 7:15 pm - 3:30 am

Mon - Sun 7:15 pm - 3:30 am

I hope I can get the JSON from each line:

[
    {
        day: Mon,
        openning_time: 7:15 pm
        closing_time: 3:30 am
    },
    ....
]

I had tried many methods but still cannot make it. Hope can get some idea

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

舟遥客 2025-01-25 04:55:46

也许这不是最佳性能解决方案,但您可以理解。
最后一部分丢失了,因为我不确定日期符号,所以我把它留给你。
基本上,您必须实现的是另一个平面图,它将日期字符串拆分为日期数组

const stringToParse = 
`Mon, Fri 2:30 pm - 8 pm / Tues 15 am - 2 pm / Weds 1:15 pm - 3:15 am
Mon, Weds - Thurs, Sat 7:15 pm - 3:30 am / Tues 4:45 pm - 5 pm / Fri 8:25 am - 9:30 pm
Mon, Weds - Sun 7:15 pm - 3:30 am
Mon - Sun 7:15 pm - 3:30 am`;

const daysArray = ['Mon', 'Tues', 'Weds', 'Thurs', 'Fri', 'Sat', 'Sun']

const json = stringToParse.split('\n')
.flatMap(line => line.split('/').map(s => s.trim()))
.map(s => {
  const [closingType, closing, _,openingType, opening, ...days] = s.split(' ').reverse()
  return {
    days: days.reverse().join(' '),
    openning_time: `${opening} ${openingType}`,
    closing_time: `${closing} ${closingType}`
  }
})

console.log(json)

Probably this is not a best performance solution but you can get the idea.
The last part is missing because I'm not sure of the day notation so I leave that to you.
Basically what you have to implement is another flatmap that splits the day string in an array of days

const stringToParse = 
`Mon, Fri 2:30 pm - 8 pm / Tues 15 am - 2 pm / Weds 1:15 pm - 3:15 am
Mon, Weds - Thurs, Sat 7:15 pm - 3:30 am / Tues 4:45 pm - 5 pm / Fri 8:25 am - 9:30 pm
Mon, Weds - Sun 7:15 pm - 3:30 am
Mon - Sun 7:15 pm - 3:30 am`;

const daysArray = ['Mon', 'Tues', 'Weds', 'Thurs', 'Fri', 'Sat', 'Sun']

const json = stringToParse.split('\n')
.flatMap(line => line.split('/').map(s => s.trim()))
.map(s => {
  const [closingType, closing, _,openingType, opening, ...days] = s.split(' ').reverse()
  return {
    days: days.reverse().join(' '),
    openning_time: `${opening} ${openingType}`,
    closing_time: `${closing} ${closingType}`
  }
})

console.log(json)

微暖i 2025-01-25 04:55:46

我建议使用 String. split() 将每个输入字符串拆分为字段。

完成后,我们将使用正则表达式将每个字段解析为日期、开放时间和关闭时间,使用 String.match()

我们还创建一个 splitDays() 函数,将诸如 Mon, Weds - Thurs 这样的日期范围转换为诸如 [Mon,Weds,Thurs] 这样的日期数组],这将使我们能够创建最终结果。

function splitDays(days) {
    return days.split(',').flatMap(splitDayRange);  
}

function splitDayRange(range) {
    const days = ['Mon', 'Tues', 'Weds', 'Thurs', 'Fri', 'Sat', 'Sun'];
    const [startIndex, endIndex] = range.trim().split(/\s*\-\s*/).map(day => days.findIndex(d => d === day));
    return days.filter((day, idx, arr) => { 
        return (idx >= startIndex) && (idx <= (endIndex || startIndex));
    });
}

function parseInput(input) {
    const parseRegEx = /([a-z\s\,\-]*)(\d+:?\d* (?:am|pm))\s*\-\s*(\d+:?\d*\s*(am|pm))/i
    return input.split(/\s*\/\s*/).flatMap(field => { 
        const [, days, opening_time, closing_time ] = field.match(parseRegEx);
        return splitDays(days).map(day => { 
            return { day, opening_time, closing_time };
        })
    });
}

let inputs = [
    'Mon, Fri 2:30 pm - 8 pm / Tues 15 am - 2 pm / Weds 1:15 pm - 3:15 am',
    'Mon, Weds - Thurs, Sat 7:15 pm - 3:30 am / Tues 4:45 pm - 5 pm / Fri 8:25 am - 9:30 pm',
    'Mon, Weds - Sun 7:15 pm - 3:30 am',
    'Mon - Sun 7:15 pm - 3:30 am'
]

for(let input of inputs) {
    console.log('Input:');
    console.log(input);
    console.log('Output:');
    console.log(parseInput(input));
} 
.as-console-wrapper { max-height: 100% !important; }

I'd suggest using String.split() to split each input string into fields.

Once this is complete we'll use a regular expression to parse each field into day, opening time and closing time using String.match().

We'd also create a splitDays() function to turn a day range like Mon, Weds - Thurs into an array of days like [Mon,Weds,Thurs], this will allow us to create the final result.

function splitDays(days) {
    return days.split(',').flatMap(splitDayRange);  
}

function splitDayRange(range) {
    const days = ['Mon', 'Tues', 'Weds', 'Thurs', 'Fri', 'Sat', 'Sun'];
    const [startIndex, endIndex] = range.trim().split(/\s*\-\s*/).map(day => days.findIndex(d => d === day));
    return days.filter((day, idx, arr) => { 
        return (idx >= startIndex) && (idx <= (endIndex || startIndex));
    });
}

function parseInput(input) {
    const parseRegEx = /([a-z\s\,\-]*)(\d+:?\d* (?:am|pm))\s*\-\s*(\d+:?\d*\s*(am|pm))/i
    return input.split(/\s*\/\s*/).flatMap(field => { 
        const [, days, opening_time, closing_time ] = field.match(parseRegEx);
        return splitDays(days).map(day => { 
            return { day, opening_time, closing_time };
        })
    });
}

let inputs = [
    'Mon, Fri 2:30 pm - 8 pm / Tues 15 am - 2 pm / Weds 1:15 pm - 3:15 am',
    'Mon, Weds - Thurs, Sat 7:15 pm - 3:30 am / Tues 4:45 pm - 5 pm / Fri 8:25 am - 9:30 pm',
    'Mon, Weds - Sun 7:15 pm - 3:30 am',
    'Mon - Sun 7:15 pm - 3:30 am'
]

for(let input of inputs) {
    console.log('Input:');
    console.log(input);
    console.log('Output:');
    console.log(parseInput(input));
} 
.as-console-wrapper { max-height: 100% !important; }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文