【问题标题】:How to split each element of a string array into different categories?如何将字符串数组的每个元素拆分为不同的类别?
【发布时间】:2021-03-29 07:10:51
【问题描述】:

我正在使用 node.js 从网站获取数据。一旦我获得了想要插入到 mySQL 数据库中的数据。获取 url 给了我一个 json 信息转储。

格式化字符串后,我使用一个数组来存储我的数据。以下是我的输出示例:

['table_id: 0xFC (252) SCTE 35',
  "section_syntax_indicator: '0'",
  "private_indicator: '0'",
  "reserved: '11'",
  'section_length: 0x39 (57)',
  'protocol_version: 0',
  'encrypted_packet: 0 no part of this message is encrypted',
  'encryption_algorithm: 0 No encryption',
  'pts_adjustment: 0xFFFF7F18 (33000) > Time: 95443.4 sec > (hh:mm:ss.ms) 26:30:43.351',
  'cw_index: 0x00 (0)',
  'tier: 0x0FFF (4095)',
  'splice_command_length: 0x0005 (5)',
  'splice_command_type: 0x06 (6) time_signal [] time_signal: > Time: 19345 sec > (hh:mm:ss.ms) 05:22:24.972',
  'time_specified_flag: 1 presence of the pts_time field',
  'reserved: 0x3F (63)',
  'pts_time: PTS: 1741047514 [0x67C646DA] > Time: 19345 sec > (hh:mm:ss.ms) 05:22:24.972',
  'descriptor_loop_length: 35 [] Descriptors: [] segmentation_descriptor (0x02): Content Identification (0x01)',
  'descriptor_tag: 0x02 (2)',
  'descriptor_length: 0x21 (33)',
  'identifier: 0x43554549 (CUEI)',
  'segmentation_event_id: 0x00000001 (1)',
  "segmentation_event_cancel_indicator: '0' a previously sent segmentation event, identified by segmentation_event_id, has NOT been cancelled",
  'reserved: 0x7F (127)',
  "program_segmentation_flag: '1' the message refers to a Program Segmentation Point and that the mode is the Program Segmentation Mode whereby all PIDs/components of the program are to be segmented",
  "segmentation_duration_flag: '0' No presence of segmentation_duration field",
  "delivery_not_restricted_flag: '1' the next five bits are reserved",
  'reserved: 0x1F (31)',
  'segmentation_upid_type: 0x01 (1) Deprecated: use type 0x0C; The segmentation_upid does not follow a standard naming scheme.',
  'segmentation_upid_length: 0x12 (18)',
  'segmentation_type_id: 0x01 (1) Content Identification',
  'segment_num: 0x01 (1)',
  'segments_expected: 0x01 (1)',
  'CRC_32: 0x46D15AF3 CRC OK'
]

我想把这个数组的每个元素分成不同的类别。

例如:"time_specified_flag: 1 presence of the pts_time field"

我希望这个元素分为 3 个不同的类别(参数、值、描述)。赞[time_specified_flag, 1, presence of the pts_time field]

编辑:这是作为字符串的原始 json 转储:"dump" : "======================================================================================================================================\r\n| PID: 0401 [SEC -> SCTE-35] length: 60[0x003C] status: VALID |\r\n--------------------------------------------------------------------------------------------------------------------------------------\r\nAddress Parameter Length Value Description\r\n--------------------------------------------------------------------------------------------------------------------------------------\r\n<span class=\"hex_tree_child\">0x0000,0: [-] PID: 0401 [SEC -> SCTE-35] <0x2A,0> : (37 nodes in 3 levels)</span>\r\n0x0000,0: * table_id <0x1,0> : 0xFC (252) SCTE 35\r\n0x0001,0: * section_syntax_indicator <0x0,1> : '0'\r\n0x0001,1: * private_indicator <0x0,1> : '0'\r\n0x0001,2: * reserved <0x0,2> : '11'\r\n0x0001,4: * section_length <0x1,4> : 0x39 (57)\r\n0x0003,0: * protocol_version <0x1,0> : 0\r\n0x0004,0: * encrypted_packet <0x0,1> : 0 no part of this message is encrypted\r\n0x0004,1: * encryption_algorithm <0x0,6> : 0 No encryption\r\n0x0004,7: * pts_adjustment <0x4,1> : 0xFFFF7F18 (-33000) => Time: 95443.4 sec => (hh:mm:ss.ms) 26:30:43.351\r\n0x0009,0: * cw_index <0x1,0> : 0x00 (0)\r\n0x000A,0: * tier <0x1,4> : 0x0FFF (4095)\r\n0x000B,4: * splice_command_length <0x1,4> : 0x0005 (5)\r\n0x000D,0: * splice_command_type <0x1,0> : 0x06 (6) time_signal\r\n<span class=\"hex_tree_child\">0x000E,0: [-] time_signal <0x5,0> : => Time: 19345 sec => (hh:mm:ss.ms) 05:22:24.972</span>\r\n0x000E,0: * time_specified_flag <0x0,1> : 1 presence of the pts_time field\r\n0x000E,1: * reserved <0x0,6> : 0x3F (63)\r\n0x000E,7: * pts_time <0x4,1> : PTS: 1741047514 [0x67C646DA] => Time: 19345 sec => (hh:mm:ss.ms) 05:22:24.972\r\n0x0013,0: * descriptor_loop_length <0x2,0> : 35\r\n<span class=\"hex_tree_child\">0x0015,0: [-] Descriptors <0x11,0> :</span>\r\n<span class=\"hex_tree_child\">0x0015,0: [-] segmentation_descriptor (0x02) <0x11,0> : Content Identification (0x01)</span>\r\n0x0015,0: * descriptor_tag <0x1,0> : 0x02 (2)\r\n0x0016,0: * descriptor_length <0x1,0> : 0x21 (33)\r\n0x0017,0: * identifier <0x4,0> : 0x43554549 (CUEI)\r\n0x001B,0: * segmentation_event_id <0x4,0> : 0x00000001 (1)\r\n0x001F,0: * segmentation_event_cancel_indicator <0x0,1> : '0' a previously sent segmentation event, identified by segmentation_event_id, has NOT been cancelled\r\n0x001F,1: * reserved <0x0,7> : 0x7F (127)\r\n0x0020,0: * program_segmentation_flag <0x0,1> : '1' the message refers to a Program Segmentation Point and that the mode is the Program Segmentation Mode whereby all PIDs/components of the program are to be segmented\r\n0x0020,1: * segmentation_duration_flag <0x0,1> : '0' No presence of segmentation_duration field\r\n0x0020,2: * delivery_not_restricted_flag <0x0,1> : '1' the next five bits are reserved\r\n0x0020,3: * reserved <0x0,5> : 0x1F (31)\r\n0x0021,0: * segmentation_upid_type <0x1,0> : 0x01 (1) Deprecated: use type 0x0C; The segmentation_upid does not follow a standard naming scheme.\r\n0x0022,0: * segmentation_upid_length <0x1,0> : 0x12 (18)\r\n0x0035,0: * segmentation_type_id <0x1,0> : 0x01 (1) Content Identification\r\n0x0036,0: * segment_num <0x1,0> : 0x01 (1)\r\n0x0037,0: * segments_expected <0x1,0> : 0x01 (1)\r\n0x0038,0: * CRC_32 <0x4,0> : 0x46D15AF3 CRC OK\r\n======================================================================================================================================\r\n",

这是我的代码 sn-p 来格式化这个数据块:

My code to format the JSON response

这是可以做到的吗?

【问题讨论】:

  • 你能分享一个来自 URL 的 JSON 的小 sn-p 以及你用来将它格式化为这个结构的代码吗?
  • 只要努力,一切皆有可能。您能否分享一些您尝试过的代码或您遇到的问题。
  • map 函数可以轻松处理这个问题。此外,使用原始的 json 结构可能更容易。那看起来像什么?
  • @maazadeeb 我更新了帖子。
  • 另外,@ToddSkelton JSON 看起来很痛苦。所以我需要格式化我只抓住重要的东西。

标签: javascript node.js arrays string


【解决方案1】:
     function threeSplit(line){
       sep1= line.indexOf(": ")
       a = line.slice(0,sep1)
       line =line.slice(sep1+2)
       sep2 = line.indexOf(" ")
       b = line.slice(0,sep2)
       c=line.slice(sep2+1)
       return [a,b,c]  
    }



[
  'table_id: 0xFC (252) SCTE 35',
  "section_syntax_indicator: '0'",
  "private_indicator: '0'",
  "reserved: '11'",
   'section_length: 0x39 (57)',
 'protocol_version: 0',
 'encrypted_packet: 0 no part of this message is encrypted',
  'encryption_algorithm: 0 No encryption',
 'pts_adjustment: 0xFFFF7F18 (33000) > Time: 95443.4 sec > (hh:mm:ss.ms) 26:30:43.351',
  'cw_index: 0x00 (0)',
  'tier: 0x0FFF (4095)',
  'splice_command_length: 0x0005 (5)',
  'splice_command_type: 0x06 (6) time_signal [] time_signal: > Time: 19345 sec > (hh:mm:ss.ms) 05:22:24.972',
  'time_specified_flag: 1 presence of the pts_time field',
  'reserved: 0x3F (63)',
  'pts_time: PTS: 1741047514 [0x67C646DA] > Time: 19345 sec > (hh:mm:ss.ms) 05:22:24.972',
 'descriptor_loop_length: 35 [] Descriptors: [] segmentation_descriptor (0x02): Content Identification (0x01)',
  'descriptor_tag: 0x02 (2)',
   'descriptor_length: 0x21 (33)',
   'identifier: 0x43554549 (CUEI)',
 'segmentation_event_id: 0x00000001 (1)',
  "segmentation_event_cancel_indicator: '0' a previously sent segmentation event, identified by segmentation_event_id, has NOT been cancelled",
  'reserved: 0x7F (127)',
   "program_segmentation_flag: '1' the message refers to a Program Segmentation Point and that the mode is the Program Segmentation Mode whereby all PIDs/components of the program are to be segmented",
   "segmentation_duration_flag: '0' No presence of segmentation_duration field",
   "delivery_not_restricted_flag: '1' the next five bits are reserved",
  'reserved: 0x1F (31)',
 'segmentation_upid_type: 0x01 (1) Deprecated: use type 0x0C; The segmentation_upid does not follow a standard naming scheme.',
   'segmentation_upid_length: 0x12 (18)',
   'segmentation_type_id: 0x01 (1) Content Identification',
 'segment_num: 0x01 (1)',
  'segments_expected: 0x01 (1)',
  'CRC_32: 0x46D15AF3 CRC OK'].map(threeSplit)

[
  [ 'table_id', '0xFC', '(252) SCTE 35' ],
  [ 'section_syntax_indicator', "'0", "'0'" ],
  [ 'private_indicator', "'0", "'0'" ],
  [ 'reserved', "'11", "'11'" ],
  [ 'section_length', '0x39', '(57)' ],
  [ 'protocol_version', '', '0' ],
  [ 'encrypted_packet', '0', 'no part of this message is encrypted' ],
  [ 'encryption_algorithm', '0', 'No encryption' ],
  [
    'pts_adjustment',
    '0xFFFF7F18',
    '(33000) > Time: 95443.4 sec > (hh:mm:ss.ms) 26:30:43.351'
  ],
  [ 'cw_index', '0x00', '(0)' ],
  [ 'tier', '0x0FFF', '(4095)' ],
  [ 'splice_command_length', '0x0005', '(5)' ],
  [
    'splice_command_type',
    '0x06',
    '(6) time_signal [] time_signal: > Time: 19345 sec > (hh:mm:ss.ms) 05:22:24.972'
  ],
  [ 'time_specified_flag', '1', 'presence of the pts_time field' ],
  [ 'reserved', '0x3F', '(63)' ],
  [
    'pts_time',
    'PTS:',
    '1741047514 [0x67C646DA] > Time: 19345 sec > (hh:mm:ss.ms) 05:22:24.972'
  ],
  [
    'descriptor_loop_length',
    '35',
    '[] Descriptors: [] segmentation_descriptor (0x02): Content Identification (0x01)'
  ],
  [ 'descriptor_tag', '0x02', '(2)' ],
  [ 'descriptor_length', '0x21', '(33)' ],
  [ 'identifier', '0x43554549', '(CUEI)' ],
  [ 'segmentation_event_id', '0x00000001', '(1)' ],
  [
    'segmentation_event_cancel_indicator',
    "'0'",
    'a previously sent segmentation event, identified by segmentation_event_id, has NOT been cancelled'
  ],
  [ 'reserved', '0x7F', '(127)' ],
  [
    'program_segmentation_flag',
    "'1'",
    'the message refers to a Program Segmentation Point and that the mode is the Program Segmentation Mode whereby all PIDs/components of the program are to be segmented'
  ],
  [
    'segmentation_duration_flag',
    "'0'",
    'No presence of segmentation_duration field'
  ],
  [
    'delivery_not_restricted_flag',
    "'1'",
    'the next five bits are reserved'
  ],
  [ 'reserved', '0x1F', '(31)' ],
  [
    'segmentation_upid_type',
    '0x01',
    '(1) Deprecated: use type 0x0C; The segmentation_upid does not follow a standard naming scheme.'
  ],
  [ 'segmentation_upid_length', '0x12', '(18)' ],
  [ 'segmentation_type_id', '0x01', '(1) Content Identification' ],
  [ 'segment_num', '0x01', '(1)' ],
  [ 'segments_expected', '0x01', '(1)' ],
  [ 'CRC_32', '0x46D15AF3', 'CRC OK' ]
]
`

【讨论】:

    【解决方案2】:

    @TayshawnHill ... 这已经足够了吗?...version 1 匹配/捕获与 pts_time 类似的格式/类别,而 version 2 匹配/捕获更通用的格式/类别。 ——彼得·塞利格

    @PeterSeliger 第 2 版按照我的意愿将它们分组。但是,我不确定如何使用正则表达式将参数、值和描述放入对象中。 – 泰肖恩希尔

    ...我们开始...

    function createKeyValueAndDescriptionList(pattern) {
      const regXCompositeValue = (/^(?<key>[^:]+)\:\s*(?:(?<value>[A-Za-z]+\:\s*[^>]+))\s*(?<description>.*)/);
      const regXGenericValue = (/^(?<key>[^:]+):\s*(?<value>[\w']+(?:\s*\([^)]*\))*)\s*(?<description>.*)/);
    
      const { groups } = (
        regXCompositeValue.exec(pattern) ||
        regXGenericValue.exec(pattern) ||
        {}
      );
      return (groups && [
    
        groups.key,
        groups.value.trim(),
        groups.description || '',
    
      ] || []);
    }
    
    console.log([
    
      'table_id: 0xFC (252) SCTE 35',
      "section_syntax_indicator: '0'",
      "private_indicator: '0'",
      "reserved: '11'",
      'section_length: 0x39 (57)',
      'protocol_version: 0',
      'encrypted_packet: 0 no part of this message is encrypted',
      'encryption_algorithm: 0 No encryption',
      'pts_adjustment: 0xFFFF7F18 (33000) > Time: 95443.4 sec > (hh:mm:ss.ms) 26:30:43.351',
      'cw_index: 0x00 (0)',
      'tier: 0x0FFF (4095)',
      'splice_command_length: 0x0005 (5)',
      'splice_command_type: 0x06 (6) time_signal [] time_signal: > Time: 19345 sec > (hh:mm:ss.ms) 05:22:24.972',
      'time_specified_flag: 1 presence of the pts_time field',
      'reserved: 0x3F (63)',
      'pts_time: PTS: 1741047514 [0x67C646DA] > Time: 19345 sec > (hh:mm:ss.ms) 05:22:24.972',
      'descriptor_loop_length: 35 [] Descriptors: [] segmentation_descriptor (0x02): Content Identification (0x01)',
      'descriptor_tag: 0x02 (2)',
      'descriptor_length: 0x21 (33)',
      'identifier: 0x43554549 (CUEI)',
      'segmentation_event_id: 0x00000001 (1)',
      "segmentation_event_cancel_indicator: '0' a previously sent segmentation event, identified by segmentation_event_id, has NOT been cancelled",
      'reserved: 0x7F (127)',
      "program_segmentation_flag: '1' the message refers to a Program Segmentation Point and that the mode is the Program Segmentation Mode whereby all PIDs/components of the program are to be segmented",
      "segmentation_duration_flag: '0' No presence of segmentation_duration field",
      "delivery_not_restricted_flag: '1' the next five bits are reserved",
      'reserved: 0x1F (31)',
      'segmentation_upid_type: 0x01 (1) Deprecated: use type 0x0C; The segmentation_upid does not follow a standard naming scheme.',
      'segmentation_upid_length: 0x12 (18)',
      'segmentation_type_id: 0x01 (1) Content Identification',
      'segment_num: 0x01 (1)',
      'segments_expected: 0x01 (1)',
      'CRC_32: 0x46D15AF3 CRC OK',
    
    ].map(createKeyValueAndDescriptionList));
    
    console.log([
    
      "foo:------------",
      "bar:++++++++++++",
      "baz:############",
    
    ].map(createKeyValueAndDescriptionList));
    .as-console-wrapper { min-height: 100%!important; top: 0; }

    【讨论】:

    • @TayshawnHill ...我提供了仍然缺少的故障处理。该方法静默失败,为每个无法匹配的模式返回一个空数组。
    猜你喜欢
    • 2017-03-12
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多