gpt4 book ai didi

javascript - 使用js将原始数据转为Array或Json

转载 作者:太空宇宙 更新时间:2023-11-04 02:10:25 26 4
gpt4 key购买 nike

如果有人可以提供帮助,那就太好了。我想将此原始数据存储到从 BEGIN:VEVENTEND:VEVENT

的数组中

原始数据

BEGIN : VEVENT\r\nDTSTART;
TZID = America / New_York : 20161231 T123000\r\nDTEND;
TZID = America / New_York : 20161231 T133000\r\nRRULE : FREQ = WEEKLY;
UNTIL = 20170211 T045959Z;
BYDAY = SA\r\nDTSTAMP : 20170228 T012109Z\r\nUID : h742jrf72h1r4oferera9dp070@google.com\r\nCREATED : 20170102 T190437Z\r\nDESCRIPTION : \r\nLAST - MODIFIED : 20170211 T152911Z\r\nLOCATION : \r\nSEQUENCE : 0 \r\nSTATUS : CONFIRMED\r\nSUMMARY : happy\r\nTRANSP : OPAQUE\r\nEND : VEVENT\r\nBEGIN : VEVENT\r\nDTSTART;
TZID = America / New_York : 20170106 T170000\r\nDTEND;
TZID = America / New_York : 20170106 T173000\r\nRRULE : FREQ = WEEKLY;
BYDAY = FR\r\nDTSTAMP : 20170228 T012109Z\r\nUID : ukad1og7htm89pc8j4di4otlk8_R20170106T220000@google.com\r\nCREATED : 20141126 T041733Z\r\nDESCRIPTION : \r\nLAST - MODIFIED : 20170120 T210013Z\r\nLOCATION : sds\r\nSEQUENCE : 0 \r\nSTATUS : CONFIRMED\r\nSUMMARY : Gold / Blue / Green Weapons\r\nTRANSP : OPAQUE\r\nBEGIN : VALARM\r\nACTION : NONE\r\nTRIGGER;
VALUE = DATE - TIME : 19760401 T005545Z\r\nEND : VALARM\r\nEND : VEVENT\r\nBEGIN : VEVENT\r\nDTSTART;
TZID = America / New_York : 20161229 T160000\r\nDTEND;
TZID = America / New_York : 20161229 T164500\r\nDTSTAMP : 20170228 T012109Z\r\nUID : 8 thf9943qdnjkcrtlvt6k8kjmg@google.com\r\nRECURRENCE - ID;
TZID = America / New_York : 20161229 T160000\r\nCREATED : 20160612 T220329Z\r\nDESCRIPTION : \r\nLAST - MODIFIED : 20170117 T200013Z\r\nLOCATION : \r\nSEQUENCE : 0 \r\nSTATUS : CONFIRMED\r\nSUMMARY : Juniors school\r\nTRANSP : OPAQUE\r\nBEGIN : VALARM\r\nACTION : NONE\r\nTRIGGER;
VALUE = DATE - TIME : 19760401 T005545Z\r\nEND : VALARM\r\nEND : VEVENT\r\n

预期输出

[
{
Hide Copy Code BEGIN: VEVENT,
DTSTART: 20160314 T093000Z,
DTEND: 20160314 T143000Z,
DTSTAMP: 20160427 T141329Z,
CREATED: 20160228 T142659Z,
DESCRIPTION: For assembler,
LAST - MODIFIED: 20160314 T133208Z,
SEQUENCE: 0,
STATUS: CONFIRMED,
END: VEVENT
}, {
Hide Copy Code BEGIN: VEVENT,
DTSTART: 20160314 T093000Z,
DTEND: 20160314 T143000Z,
DTSTAMP: 20160427 T141329Z,
CREATED: 20160228 T142659Z,
DESCRIPTION: For assembler,
LAST - MODIFIED: 20160314 T133208Z,
SEQUENCE: 0,
STATUS: CONFIRMED,
END: VEVENT
}
]

我发现它在 ruby 中有效。我尝试了一些将 icasdata 转换为 Json 的 js api,但它无法获取准确的数据,所以我在想是否可以读取原始数据并将其传递给数组而无需任何 API。对上述问题的任何建议都会有帮助。谢谢。

最佳答案

我不完全确定你想要什么,但根据我的理解,如果原始数据是字符串,并且你想要标有 END: VEVENT 的 json 数组。这可能是一个很好的起点。我希望这对您有帮助,即使是以最小的方式:

var rawData = "BEGIN:VEVENT\r\nDTSTART;TZID=America/New_York:20161231T123000\r\nDTEND;TZID=America/New_York:20161231T133000\r\nRRULE:FREQ=WEEKLY;UNTIL=20170211T045959Z;BYDAY=SA\r\nDTSTAMP:20170228T012109Z\r\nUID:h742jrf72h1r4oferera9dp070@google.com\r\nCREATED:20170102T190437Z\r\nDESCRIPTION:\r\nLAST-MODIFIED:20170211T152911Z\r\nLOCATION:\r\nSEQUENCE:0\r\nSTATUS:CONFIRMED\r\nSUMMARY:happy\r\nTRANSP:OPAQUE\r\nEND:VEVENT\r\nBEGIN:VEVENT\r\nDTSTART;TZID=America/New_York:20170106T170000\r\nDTEND;TZID=America/New_York:20170106T173000\r\nRRULE:FREQ=WEEKLY;BYDAY=FR\r\nDTSTAMP:20170228T012109Z\r\nUID:ukad1og7htm89pc8j4di4otlk8_R20170106T220000@google.com\r\nCREATED:20141126T041733Z\r\nDESCRIPTION:\r\nLAST-MODIFIED:20170120T210013Z\r\nLOCATION:sds\r\nSEQUENCE:0\r\nSTATUS:CONFIRMED\r\nSUMMARY:Gold/Blue/Green Weapons\r\nTRANSP:OPAQUE\r\nBEGIN:VALARM\r\nACTION:NONE\r\nTRIGGER;VALUE=DATE-TIME:19760401T005545Z\r\nEND:VALARM\r\nEND:VEVENT\r\nBEGIN:VEVENT\r\nDTSTART;TZID=America/New_York:20161229T160000\r\nDTEND;TZID=America/New_York:20161229T164500\r\nDTSTAMP:20170228T012109Z\r\nUID:8thf9943qdnjkcrtlvt6k8kjmg@google.com\r\nRECURRENCE-ID;TZID=America/New_York:20161229T160000\r\nCREATED:20160612T220329Z\r\nDESCRIPTION:\r\nLAST-MODIFIED:20170117T200013Z\r\nLOCATION:\r\nSEQUENCE:0\r\nSTATUS:CONFIRMED\r\nSUMMARY:Juniors school\r\nTRANSP:OPAQUE\r\nBEGIN:VALARM\r\nACTION:NONE\r\nTRIGGER;VALUE=DATE-TIME:19760401T005545Z\r\nEND:VALARM\r\nEND:VEVENT\r\n"

function rawToJSON(raw) {

var arr = [];
var lines = raw.split('\r\n');

lines.reduce(function(obj, line){

var keyValue = line.split(":");
var key = keyValue[0];
var value = keyValue[1];

Object.assign(obj, {[key]: value});

if (key == 'END' && value == 'VEVENT') {
arr.push(obj);
return {};
}

return obj;

}, {});

return arr;

}

console.log(rawToJSON(rawData));

关于javascript - 使用js将原始数据转为Array或Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42523234/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com