当前位置: 首页 > 知识库问答 >
问题:

使用json条目进行csv解析

濮阳俊明
2023-03-14

我正在编写我的第一个Haskell程序,试图解析文件中的“csv”。到目前为止,我已经写了以下内容


{-#  LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleInstances #-}

module Main where

import Lib
import System.IO
import Control.Applicative
import Control.Monad
import GHC.Generics (Generic)
import Data.Text    (Text)
import Text.Regex    (subRegex,mkRegex)
import qualified Data.Text  as Text
import qualified Data.ByteString    as BS
import qualified Data.Csv  as DC
-- import Data.Csv  (.!)
import qualified Data.Aeson  as DA
import qualified Data.Vector as V
import qualified Data.ByteString.Lazy.UTF8 as BLU -- from utf8-string
import qualified Data.ByteString.UTF8 as BU -- as toString :: ByteString -> String

testEmtecString = "2020-01-02 07:57:31+00,\"[{\"\"rpm\"\": 893, \"\"geom\"\": \"\"0101000020E6100000B554DE8E70DAE9BF6F9EEA909BE74940\"\", \"\"speed\"\": 46.670976, \"\"airflow\"\": 8.13, \"\"g_force\"\": 0, \"\"accuracy\"\": \"\"FixOk\"\", \"\"ignition\"\": 1, \"\"latitude\"\": 51.809435, \"\"odometer\"\": 4867, \"\"longitude\"\": -0.807915, \"\"speed_gps\"\": 0, \"\"trip_event\"\": null, \"\"gpsdatatime\"\": \"\"2020-01-02T07:57:31+00:00\"\", \"\"harsh_force\"\": 0.25, \"\"speed_miles\"\": 28.99999989558144, \"\"trip_number\"\": null, \"\"acceleration\"\": 0, \"\"braking_alarm\"\": false, \"\"fuel_consumed\"\": 22563, \"\"harsh_braking\"\": false, \"\"odometer_miles\"\": 3024.21358173, \"\"cornering_alarm\"\": null, \"\"harsh_cornering\"\": false, \"\"speed_gps_miles\"\": 0.0, \"\"throttle_position\"\": 14, \"\"acceleration_alarm\"\": false, \"\"harsh_acceleration\"\": true, \"\"coolant_temperature\"\": 29, \"\"intake_air_temperature\"\": 8, \"\"harsh_cornering_degrees\"\": 0, \"\"manifold_absolute_pressure\"\": 97}, {\"\"rpm\"\": 893, \"\"geom\"\": \"\"0101000020E610000047382D78D1D7E9BF61545227A0E74940\"\", \"\"speed\"\": 27.358848000000002, \"\"airflow\"\": 8.13, \"\"g_force\"\": 0.6795786612300374, \"\"accuracy\"\": \"\"FixOk\"\", \"\"ignition\"\": 1, \"\"latitude\"\": 51.809575, \"\"odometer\"\": 4867, \"\"longitude\"\": -0.807595, \"\"speed_gps\"\": 24, \"\"trip_event\"\": null, \"\"gpsdatatime\"\": \"\"2020-01-02T07:57:35+00:00\"\", \"\"harsh_force\"\": 0.25, \"\"speed_miles\"\": 16.99999993878912, \"\"trip_number\"\": null, \"\"acceleration\"\": 6.666666666666667, \"\"braking_alarm\"\": false, \"\"fuel_consumed\"\": 22563, \"\"harsh_braking\"\": false, \"\"odometer_miles\"\": 3024.21358173, \"\"cornering_alarm\"\": null, \"\"harsh_cornering\"\": false, \"\"speed_gps_miles\"\": 14.912908559999998, \"\"gps_braking_alarm\"\": true, \"\"throttle_position\"\": 14, \"\"acceleration_alarm\"\": false, \"\"harsh_acceleration\"\": true, \"\"coolant_temperature\"\": 29, \"\"gps_acceleration_alarm\"\": false, \"\"intake_air_temperature\"\": 8, \"\"harsh_cornering_degrees\"\": 0, \"\"manifold_absolute_pressure\"\": 97}, {\"\"rpm\"\": 1178, \"\"geom\"\": \"\"0101000020E61000000611A96917D3E9BF9E0B23BDA8E74940\"\", \"\"speed\"\": 46.670976, \"\"airflow\"\": 15.69, \"\"g_force\"\": -0.3397893306150187, \"\"accuracy\"\": \"\"FixOk\"\", \"\"ignition\"\": 1, \"\"latitude\"\": 51.809837, \"\"odometer\"\": 4867, \"\"longitude\"\": -0.807018, \"\"speed_gps\"\": 12, \"\"trip_event\"\": null, \"\"gpsdatatime\"\": \"\"2020-01-02T07:57:50+00:00\"\", \"\"harsh_force\"\": 0.25, \"\"speed_miles\"\": 28.99999989558144, \"\"trip_number\"\": null, \"\"acceleration\"\": -3.3333333333333335, \"\"braking_alarm\"\": false, \"\"fuel_consumed\"\": 22565, \"\"harsh_braking\"\": false, \"\"odometer_miles\"\": 3024.21358173, \"\"cornering_alarm\"\": null, \"\"harsh_cornering\"\": false, \"\"speed_gps_miles\"\": 7.456454279999999, \"\"gps_braking_alarm\"\": false, \"\"throttle_position\"\": 32, \"\"acceleration_alarm\"\": false, \"\"harsh_acceleration\"\": true, \"\"coolant_temperature\"\": 31, \"\"gps_acceleration_alarm\"\": false, \"\"intake_air_temperature\"\": 11, \"\"harsh_cornering_degrees\"\": 0, \"\"manifold_absolute_pressure\"\": 116}, {\"\"rpm\"\": 1400, \"\"geom\"\": \"\"0101000020E6100000ED116A8654D1E9BFE17F2BD9B1E74940\"\", \"\"speed\"\": 51.499008, \"\"airflow\"\": 21.97, \"\"g_force\"\": 0.5379997734737796, \"\"accuracy\"\": \"\"FixOk\"\", \"\"ignition\"\": 1, \"\"latitude\"\": 51.810115, \"\"odometer\"\": 4867, \"\"longitude\"\": -0.806803, \"\"speed_gps\"\": 31, \"\"trip_event\"\": null, \"\"gpsdatatime\"\": \"\"2020-01-02T07:57:54+00:00\"\", \"\"harsh_force\"\": 0.25, \"\"speed_miles\"\": 31.99999988477952, \"\"trip_number\"\": null, \"\"acceleration\"\": 5.277777777777778, \"\"braking_alarm\"\": false, \"\"fuel_consumed\"\": 22565, \"\"harsh_braking\"\": false, \"\"odometer_miles\"\": 3024.21358173, \"\"cornering_alarm\"\": null, \"\"harsh_cornering\"\": false, \"\"speed_gps_miles\"\": 19.262506889999997, \"\"gps_braking_alarm\"\": true, \"\"throttle_position\"\": 36, \"\"acceleration_alarm\"\": false, \"\"harsh_acceleration\"\": true, \"\"coolant_temperature\"\": 32, \"\"gps_acceleration_alarm\"\": false, \"\"intake_air_temperature\"\": 12, \"\"harsh_cornering_degrees\"\": 0, \"\"manifold_absolute_pressure\"\": 137}, {\"\"rpm\"\": 901, \"\"geom\"\": \"\"0101000020E61000002ECBD765F8CFE9BFF10D85CFD6E74940\"\", \"\"speed\"\": 35.405568, \"\"airflow\"\": 8.24, \"\"g_force\"\": -0.02831577755125156, \"\"accuracy\"\": \"\"FixOk\"\", \"\"ignition\"\": 1, \"\"latitude\"\": 51.811243, \"\"odometer\"\": 4868, \"\"longitude\"\": -0.806637, \"\"speed_gps\"\": 30, \"\"trip_event\"\": null, \"\"gpsdatatime\"\": \"\"2020-01-02T07:58:09+00:00\"\", \"\"harsh_force\"\": 0.25, \"\"speed_miles\"\": 21.99999992078592, \"\"trip_number\"\": null, \"\"acceleration\"\": -0.2777777777777778, \"\"braking_alarm\"\": false, \"\"fuel_consumed\"\": 22565, \"\"harsh_braking\"\": false, \"\"odometer_miles\"\": 3024.83495292, \"\"cornering_alarm\"\": null, \"\"harsh_cornering\"\": false, \"\"speed_gps_miles\"\": 18.6411357, \"\"gps_braking_alarm\"\": false, \"\"throttle_position\"\": 14, \"\"acceleration_alarm\"\": false, \"\"harsh_acceleration\"\": true, \"\"coolant_temperature\"\": 33, \"\"gps_acceleration_alarm\"\": false, \"\"intake_air_temperature\"\": 8, \"\"harsh_cornering_degrees\"\": 0, \"\"manifold_absolute_pressure\"\": 96}, {\"\"rpm\"\": 897, \"\"geom\"\": \"\"0101000020E6100000471FF30181CEE9BFEC504D49D6E74940\"\", \"\"speed\"\": 22.530816, \"\"airflow\"\": 8.13, \"\"g_force\"\": 0.42473666326877335, \"\"accuracy\"\": \"\"FixOk\"\", \"\"ignition\"\": 1, \"\"latitude\"\": 51.811227, \"\"odometer\"\": 4868, \"\"longitude\"\": -0.806458, \"\"speed_gps\"\": 45, \"\"trip_event\"\": null, \"\"gpsdatatime\"\": \"\"2020-01-02T07:58:10+00:00\"\", \"\"harsh_force\"\": 0.25, \"\"speed_miles\"\": 13.99999994959104, \"\"trip_number\"\": null, \"\"acceleration\"\": 4.166666666666667, \"\"braking_alarm\"\": false, \"\"fuel_consumed\"\": 22565, \"\"harsh_braking\"\": false, \"\"odometer_miles\"\": 3024.83495292, \"\"cornering_alarm\"\": null, \"\"harsh_cornering\"\": false, \"\"speed_gps_miles\"\": 27.96170355, \"\"gps_braking_alarm\"\": true, \"\"throttle_position\"\": 14, \"\"acceleration_alarm\"\": false, \"\"harsh_acceleration\"\": true, \"\"coolant_temperature\"\": 33, \"\"gps_acceleration_alarm\"\": false, \"\"intake_air_temperature\"\": 8, \"\"harsh_cornering_degrees\"\": 0, \"\"manifold_absolute_pressure\"\": 97}, {\"\"rpm\"\": 1589, \"\"geom\"\": \"\"0101000020E610000025917D9065C1E9BFE09C11A5BDE74940\"\", \"\"speed\"\": 64.37376, \"\"airflow\"\": 20.72, \"\"g_force\"\": -0.7928417714350436, \"\"accuracy\"\": \"\"FixOk\"\", \"\"ignition\"\": 1, \"\"latitude\"\": 51.810475, \"\"odometer\"\": 4868, \"\"longitude\"\": -0.804858, \"\"speed_gps\"\": 17, \"\"trip_event\"\": null, \"\"gpsdatatime\"\": \"\"2020-01-02T07:58:40+00:00\"\", \"\"harsh_force\"\": 0.25, \"\"speed_miles\"\": 39.9999998559744, \"\"trip_number\"\": null, \"\"acceleration\"\": -7.777777777777779, \"\"braking_alarm\"\": false, \"\"fuel_consumed\"\": 22568, \"\"harsh_braking\"\": false, \"\"odometer_miles\"\": 3024.83495292, \"\"cornering_alarm\"\": null, \"\"harsh_cornering\"\": false, \"\"speed_gps_miles\"\": 10.563310229999999, \"\"gps_braking_alarm\"\": false, \"\"throttle_position\"\": 33, \"\"acceleration_alarm\"\": false, \"\"harsh_acceleration\"\": true, \"\"coolant_temperature\"\": 36, \"\"gps_acceleration_alarm\"\": false, \"\"intake_air_temperature\"\": 11, \"\"harsh_cornering_degrees\"\": 0, \"\"manifold_absolute_pressure\"\": 131}, {\"\"rpm\"\": 259, \"\"geom\"\": \"\"0101000020E6100000AD307DAF21B8E9BF8CA03193A8E74940\"\", \"\"speed\"\": 22.530816, \"\"airflow\"\": 0.77, \"\"g_force\"\": 0.509683995922528, \"\"accuracy\"\": \"\"FixOk\"\", \"\"ignition\"\": 1, \"\"latitude\"\": 51.809832, \"\"odometer\"\": 4868, \"\"longitude\"\": -0.803727, \"\"speed_gps\"\": 35, \"\"trip_event\"\": null, \"\"gpsdatatime\"\": \"\"2020-01-02T07:58:51+00:00\"\", \"\"harsh_force\"\": 0.25, \"\"speed_miles\"\": 13.99999994959104, \"\"trip_number\"\": null, \"\"acceleration\"\": 5.0, \"\"braking_alarm\"\": false, \"\"fuel_consumed\"\": 22568, \"\"harsh_braking\"\": false, \"\"odometer_miles\"\": 3024.83495292, \"\"cornering_alarm\"\": null, \"\"harsh_cornering\"\": false, \"\"speed_gps_miles\"\": 21.74799165, \"\"gps_braking_alarm\"\": true, \"\"throttle_position\"\": 14, \"\"acceleration_alarm\"\": false, \"\"harsh_acceleration\"\": true, \"\"coolant_temperature\"\": 37, \"\"gps_acceleration_alarm\"\": false, \"\"intake_air_temperature\"\": 5, \"\"harsh_cornering_degrees\"\": 0, \"\"manifold_absolute_pressure\"\": 50}, {\"\"rpm\"\": 935, \"\"geom\"\": \"\"0101000020E61000009F1F46088FB6E9BF21EA3E00A9E74940\"\", \"\"speed\"\": 17.702784, \"\"airflow\"\": 7.8, \"\"g_force\"\": -0.7078944387812889, \"\"accuracy\"\": \"\"FixOk\"\", \"\"ignition\"\": 1, \"\"latitude\"\": 51.809845, \"\"odometer\"\": 4868, \"\"longitude\"\": -0.803535, \"\"speed_gps\"\": 10, \"\"trip_event\"\": null, \"\"gpsdatatime\"\": \"\"2020-01-02T07:58:56+00:00\"\", \"\"harsh_force\"\": 0.25, \"\"speed_miles\"\": 10.99999996039296, \"\"trip_number\"\": null, \"\"acceleration\"\": -6.944444444444445, \"\"braking_alarm\"\": false, \"\"fuel_consumed\"\": 22568, \"\"harsh_braking\"\": false, \"\"odometer_miles\"\": 3024.83495292, \"\"cornering_alarm\"\": null, \"\"harsh_cornering\"\": false, \"\"speed_gps_miles\"\": 6.2137119, \"\"gps_braking_alarm\"\": false, \"\"throttle_position\"\": 21, \"\"acceleration_alarm\"\": false, \"\"harsh_acceleration\"\": true, \"\"coolant_temperature\"\": 37, \"\"gps_acceleration_alarm\"\": false, \"\"intake_air_temperature\"\": 12, \"\"harsh_cornering_degrees\"\": 0, \"\"manifold_absolute_pressure\"\": 90}, {\"\"rpm\"\": 935, \"\"geom\"\": \"\"0101000020E610000002BB9A3C65B5E9BF9291B3B0A7E74940\"\", \"\"speed\"\": 12.874752, \"\"airflow\"\": 7.8, \"\"g_force\"\": -0.05663155510250312, \"\"accuracy\"\": \"\"FixOk\"\", \"\"ignition\"\": 1, \"\"latitude\"\": 51.809805, \"\"odometer\"\": 4868, \"\"longitude\"\": -0.803393, \"\"speed_gps\"\": 8, \"\"trip_event\"\": null, \"\"gpsdatatime\"\": \"\"2020-01-02T07:59:01+00:00\"\", \"\"harsh_force\"\": 0.25, \"\"speed_miles\"\": 7.99999997119488, \"\"trip_number\"\": null, \"\"acceleration\"\": -0.5555555555555556, \"\"braking_alarm\"\": false, \"\"fuel_consumed\"\": 22568, \"\"harsh_braking\"\": false, \"\"odometer_miles\"\": 3024.83495292, \"\"cornering_alarm\"\": null, \"\"harsh_cornering\"\": false, \"\"speed_gps_miles\"\": 4.97096952, \"\"gps_braking_alarm\"\": false, \"\"throttle_position\"\": 21, \"\"acceleration_alarm\"\": false, \"\"harsh_acceleration\"\": true, \"\"coolant_temperature\"\": 37, \"\"gps_acceleration_alarm\"\": false, \"\"intake_air_temperature\"\": 12, \"\"harsh_cornering_degrees\"\": 0, \"\"manifold_absolute_pressure\"\": 90}, {\"\"rpm\"\": 350, \"\"geom\"\": \"\"0101000020E61000003BA92F4B3BB5E9BFCEC3094CA7E74940\"\", \"\"speed\"\": 0.0, \"\"airflow\"\": 0.0, \"\"g_force\"\": -0.14157888775625777, \"\"accuracy\"\": \"\"FixOk\"\", \"\"ignition\"\": 1, \"\"latitude\"\": 51.809793, \"\"odometer\"\": 4868, \"\"longitude\"\": -0.803373, \"\"speed_gps\"\": 3, \"\"trip_event\"\": null, \"\"gpsdatatime\"\": \"\"2020-01-02T07:59:03+00:00\"\", \"\"harsh_force\"\": 0.25, \"\"speed_miles\"\": 0.0, \"\"trip_number\"\": null, \"\"acceleration\"\": -1.3888888888888888, \"\"braking_alarm\"\": false, \"\"fuel_consumed\"\": 22568, \"\"harsh_braking\"\": false, \"\"odometer_miles\"\": 3024.83495292, \"\"cornering_alarm\"\": null, \"\"harsh_cornering\"\": false, \"\"speed_gps_miles\"\": 1.8641135699999998, \"\"gps_braking_alarm\"\": false, \"\"throttle_position\"\": 14, \"\"acceleration_alarm\"\": false, \"\"harsh_acceleration\"\": true, \"\"coolant_temperature\"\": 38, \"\"gps_acceleration_alarm\"\": false, \"\"intake_air_temperature\"\": 10, \"\"harsh_cornering_degrees\"\": 0, \"\"manifold_absolute_pressure\"\": 36}, {\"\"rpm\"\": 350, \"\"geom\"\": \"\"0101000020E61000003BA92F4B3BB5E9BFCEC3094CA7E74940\"\", \"\"speed\"\": 0.0, \"\"airflow\"\": 0.0, \"\"g_force\"\": -0.08494733265375468, \"\"accuracy\"\": \"\"FixOk\"\", \"\"ignition\"\": 1, \"\"latitude\"\": 51.809793, \"\"odometer\"\": 4868, \"\"longitude\"\": -0.803373, \"\"speed_gps\"\": 0, \"\"trip_event\"\": null, \"\"gpsdatatime\"\": \"\"2020-01-02T07:59:07+00:00\"\", \"\"harsh_force\"\": 0.25, \"\"speed_miles\"\": 0.0, \"\"trip_number\"\": null, \"\"acceleration\"\": -0.8333333333333334, \"\"braking_alarm\"\": false, \"\"fuel_consumed\"\": 22569, \"\"harsh_braking\"\": false, \"\"odometer_miles\"\": 3024.83495292, \"\"cornering_alarm\"\": null, \"\"harsh_cornering\"\": false, \"\"speed_gps_miles\"\": 0.0, \"\"gps_braking_alarm\"\": false, \"\"throttle_position\"\": 14, \"\"acceleration_alarm\"\": false, \"\"harsh_acceleration\"\": true, \"\"coolant_temperature\"\": 38, \"\"gps_acceleration_alarm\"\": false, \"\"intake_air_temperature\"\": 10, \"\"harsh_cornering_degrees\"\": 0, \"\"manifold_absolute_pressure\"\": 36}, {\"\"rpm\"\": 350, \"\"geom\"\": \"\"0101000020E61000003BA92F4B3BB5E9BFCEC3094CA7E74940\"\", \"\"speed\"\": 0.0, \"\"airflow\"\": 0.0, \"\"g_force\"\": 0.0, \"\"accuracy\"\": \"\"FixOk\"\", \"\"ignition\"\": 1, \"\"latitude\"\": 51.809793, \"\"odometer\"\": 4868, \"\"longitude\"\": -0.803373, \"\"speed_gps\"\": 0, \"\"trip_event\"\": null, \"\"gpsdatatime\"\": \"\"2020-01-02T07:59:22+00:00\"\", \"\"harsh_force\"\": 0.25, \"\"speed_miles\"\": 0.0, \"\"trip_number\"\": null, \"\"acceleration\"\": 0.0, \"\"braking_alarm\"\": false, \"\"fuel_consumed\"\": 22573, \"\"harsh_braking\"\": false, \"\"odometer_miles\"\": 3024.83495292, \"\"cornering_alarm\"\": null, \"\"harsh_cornering\"\": false, \"\"speed_gps_miles\"\": 0.0, \"\"gps_braking_alarm\"\": false, \"\"throttle_position\"\": 14, \"\"acceleration_alarm\"\": false, \"\"harsh_acceleration\"\": true, \"\"coolant_temperature\"\": 38, \"\"gps_acceleration_alarm\"\": false, \"\"intake_air_temperature\"\": 10, \"\"harsh_cornering_degrees\"\": 0, \"\"manifold_absolute_pressure\"\": 36}, {\"\"rpm\"\": 350, \"\"geom\"\": \"\"0101000020E61000003BA92F4B3BB5E9BFCEC3094CA7E74940\"\", \"\"speed\"\": 0.0, \"\"airflow\"\": 0.0, \"\"g_force\"\": 0.0, \"\"accuracy\"\": \"\"FixOk\"\", \"\"ignition\"\": 1, \"\"latitude\"\": 51.809793, \"\"odometer\"\": 4868, \"\"longitude\"\": -0.803373, \"\"speed_gps\"\": 0, \"\"trip_event\"\": null, \"\"gpsdatatime\"\": \"\"2020-01-02T07:59:37+00:00\"\", \"\"harsh_force\"\": 0.25, \"\"speed_miles\"\": 0.0, \"\"trip_number\"\": null, \"\"acceleration\"\": 0.0, \"\"braking_alarm\"\": false, \"\"fuel_consumed\"\": 22577, \"\"harsh_braking\"\": false, \"\"odometer_miles\"\": 3024.83495292, \"\"cornering_alarm\"\": null, \"\"harsh_cornering\"\": false, \"\"speed_gps_miles\"\": 0.0, \"\"gps_braking_alarm\"\": false, \"\"throttle_position\"\": 14, \"\"acceleration_alarm\"\": false, \"\"harsh_acceleration\"\": true, \"\"coolant_temperature\"\": 38, \"\"gps_acceleration_alarm\"\": false, \"\"intake_air_temperature\"\": 10, \"\"harsh_cornering_degrees\"\": 0, \"\"manifold_absolute_pressure\"\": 36}, {\"\"rpm\"\": 350, \"\"geom\"\": \"\"0101000020E61000003BA92F4B3BB5E9BFCEC3094CA7E74940\"\", \"\"speed\"\": 0.0, \"\"airflow\"\": 0.0, \"\"g_force\"\": 0.0, \"\"accuracy\"\": \"\"FixOk\"\", \"\"ignition\"\": 1, \"\"latitude\"\": 51.809793, \"\"odometer\"\": 4868, \"\"longitude\"\": -0.803373, \"\"speed_gps\"\": 0, \"\"trip_event\"\": null, \"\"gpsdatatime\"\": \"\"2020-01-02T07:59:52+00:00\"\", \"\"harsh_force\"\": 0.25, \"\"speed_miles\"\": 0.0, \"\"trip_number\"\": null, \"\"acceleration\"\": 0.0, \"\"braking_alarm\"\": false, \"\"fuel_consumed\"\": 22582, \"\"harsh_braking\"\": false, \"\"odometer_miles\"\": 3024.83495292, \"\"cornering_alarm\"\": null, \"\"harsh_cornering\"\": false, \"\"speed_gps_miles\"\": 0.0, \"\"gps_braking_alarm\"\": false, \"\"throttle_position\"\": 14, \"\"acceleration_alarm\"\": false, \"\"harsh_acceleration\"\": true, \"\"coolant_temperature\"\": 38, \"\"gps_acceleration_alarm\"\": false, \"\"intake_air_temperature\"\": 10, \"\"harsh_cornering_degrees\"\": 0, \"\"manifold_absolute_pressure\"\": 36}, {\"\"rpm\"\": 350, \"\"geom\"\": \"\"0101000020E61000003BA92F4B3BB5E9BFCEC3094CA7E74940\"\", \"\"speed\"\": 0.0, \"\"airflow\"\": 0.0, \"\"g_force\"\": 0.0, \"\"accuracy\"\": \"\"FixOk\"\", \"\"ignition\"\": 1, \"\"latitude\"\": 51.809793, \"\"odometer\"\": 4868, \"\"longitude\"\": -0.803373, \"\"speed_gps\"\": 0, \"\"trip_event\"\": null, \"\"gpsdatatime\"\": \"\"2020-01-02T08:00:03+00:00\"\", \"\"harsh_force\"\": 0.25, \"\"speed_miles\"\": 0.0, \"\"trip_number\"\": null, \"\"acceleration\"\": 0.0, \"\"braking_alarm\"\": false, \"\"fuel_consumed\"\": 22584, \"\"harsh_braking\"\": false, \"\"odometer_miles\"\": 3024.83495292, \"\"cornering_alarm\"\": null, \"\"harsh_cornering\"\": false, \"\"speed_gps_miles\"\": 0.0, \"\"gps_braking_alarm\"\": false, \"\"throttle_position\"\": 14, \"\"acceleration_alarm\"\": false, \"\"harsh_acceleration\"\": true, \"\"coolant_temperature\"\": 38, \"\"gps_acceleration_alarm\"\": false, \"\"intake_air_temperature\"\": 10, \"\"harsh_cornering_degrees\"\": 0, \"\"manifold_absolute_pressure\"\": 36}, {\"\"rpm\"\": 350, \"\"geom\"\": \"\"0101000020E61000003BA92F4B3BB5E9BFCEC3094CA7E74940\"\", \"\"speed\"\": 0.0, \"\"airflow\"\": 0.0, \"\"g_force\"\": 0.0, \"\"accuracy\"\": \"\"FixOk\"\", \"\"ignition\"\": 1, \"\"latitude\"\": 51.809793, \"\"odometer\"\": 4868, \"\"longitude\"\": -0.803373, \"\"speed_gps\"\": 0, \"\"trip_event\"\": null, \"\"gpsdatatime\"\": \"\"2020-01-02T08:00:32+00:00\"\", \"\"harsh_force\"\": 0.25, \"\"speed_miles\"\": 0.0, \"\"trip_number\"\": null, \"\"acceleration\"\": 0.0, \"\"braking_alarm\"\": false, \"\"fuel_consumed\"\": 22584, \"\"harsh_braking\"\": false, \"\"odometer_miles\"\": 3024.83495292, \"\"cornering_alarm\"\": null, \"\"harsh_cornering\"\": false, \"\"speed_gps_miles\"\": 0.0, \"\"gps_braking_alarm\"\": false, \"\"throttle_position\"\": 14, \"\"acceleration_alarm\"\": false, \"\"harsh_acceleration\"\": true, \"\"coolant_temperature\"\": 38, \"\"gps_acceleration_alarm\"\": false, \"\"intake_air_temperature\"\": 10, \"\"harsh_cornering_degrees\"\": 0, \"\"manifold_absolute_pressure\"\": 36}, {\"\"rpm\"\": 350, \"\"geom\"\": \"\"0101000020E61000003BA92F4B3BB5E9BFCEC3094CA7E74940\"\", \"\"speed\"\": 0.0, \"\"airflow\"\": 0.0, \"\"g_force\"\": 0.0, \"\"accuracy\"\": \"\"FixOk\"\", \"\"ignition\"\": 1, \"\"latitude\"\": 51.809793, \"\"odometer\"\": 4868, \"\"longitude\"\": -0.803373, \"\"speed_gps\"\": 0, \"\"trip_event\"\": null, \"\"gpsdatatime\"\": \"\"2020-01-02T08:00:59+00:00\"\", \"\"harsh_force\"\": 0.25, \"\"speed_miles\"\": 0.0, \"\"trip_number\"\": null, \"\"acceleration\"\": 0.0, \"\"braking_alarm\"\": false, \"\"fuel_consumed\"\": 22584, \"\"harsh_braking\"\": false, \"\"odometer_miles\"\": 3024.83495292, \"\"cornering_alarm\"\": null, \"\"harsh_cornering\"\": false, \"\"speed_gps_miles\"\": 0.0, \"\"gps_braking_alarm\"\": false, \"\"throttle_position\"\": 14, \"\"acceleration_alarm\"\": false, \"\"harsh_acceleration\"\": true, \"\"coolant_temperature\"\": 38, \"\"gps_acceleration_alarm\"\": false, \"\"intake_air_temperature\"\": 10, \"\"harsh_cornering_degrees\"\": 0, \"\"manifold_absolute_pressure\"\": 36}]\""
-- "
testString1 = "{\"rpm\": 893, \"geom\": \"0101000020E6100000B554DE8E70DAE9BF6F9EEA909BE74940\", \"speed\": 46.670976, \"airflow\": 8.13, \"g_force\": 0, \"accuracy\": \"FixOk\", \"ignition\": 1, \"latitude\": 51.809435, \"odometer\": 4867, \"longitude\": -0.807915, \"speed_gps\": 0, \"trip_event\": null, \"gpsdatatime\": \"2020-01-02T07:57:31+00:00\", \"harsh_force\": 0.25, \"speed_miles\": 28.99999989558144, \"trip_number\": null, \"acceleration\": 0, \"braking_alarm\": false, \"fuel_consumed\": 22563, \"harsh_braking\": false, \"odometer_miles\": 3024.21358173, \"cornering_alarm\": null, \"harsh_cornering\": false, \"speed_gps_miles\": 0.0, \"throttle_position\": 14, \"acceleration_alarm\": false, \"harsh_acceleration\": true, \"coolant_temperature\": 29, \"intake_air_temperature\": 8, \"harsh_cornering_degrees\": 0, \"manifold_absolute_pressure\": 97} "

data EmtecRecord = EmtecRecord
   {
    rpm ::  Maybe Int ,
    geom ::  Text,
    speed ::  Maybe Double
   } deriving (Generic, Show)


data EmtecCSV  =
  EmtecCSV
    {
    date :: Text , jsonData :: Maybe EmtecRecord
    } deriving (Generic,Show)


instance DA.FromJSON EmtecRecord

regex =  mkRegex "\"\""

colapseQuotes s =  subRegex regex  s "\""

decoder :: BS.ByteString -> Maybe EmtecRecord
decoder = DA.decode . BLU.fromString . colapseQuotes . BU.toString

instance DC.FromField (Maybe EmtecRecord) where
  parseField   =  pure . decoder


instance DC.FromRecord EmtecCSV
  where
    parseRecord m  =  EmtecCSV <$> (m DC..! 0)  <*>  (m DC..! 1)  --( ::  (DC.Parser (Maybe EmtecRecord)  ) )


csvParser :: BLU.ByteString  -> Either String (V.Vector  EmtecCSV)
csvParser s = DC.decode  DC.NoHeader s

dec =  colapseQuotes
main :: IO ()
main =  print  (csvParser   testEmtecString     )

我设法解析json或csv,但将这些东西粘在一起是我目前对Haskell的理解(我是一个非常新手)的一种方式。我认为问题是我没有属性实例化DC。从记录中我得到一个错误

• Overlapping instances for DC.FromField (Maybe EmtecRecord)
    arising from a use of ‘DC..!’
  Matching instances:
    instance DC.FromField a => DC.FromField (Maybe a)
      -- Defined in ‘cassava-0.5.2.0:Data.Csv.Conversion’
    instance DC.FromField (Maybe EmtecRecord)
      -- Defined at app/Main.hs:60:10
• In the second argument of ‘(<*>)’, namely ‘(m DC..! 1)’
  In the expression: EmtecCSV <$> (m DC..! 0) <*> (m DC..! 1)
  In an equation for ‘DC.parseRecord’:
      DC.parseRecord m = EmtecCSV <$> (m DC..! 0) <*> (m DC..! 1)

|66|parseBook m=EmtecCSV

Any insites of how can i overcome that obstacle ?

共有1个答案

丘浩宕
2023-03-14

更改parseField=pure。解码器toparseField=可能为空。解码器,然后将所有出现的可能是EmtecRecord替换为EmtecRecord

 类似资料:
  • 问题内容: 我想从类型为的JSON解析数据。我正在使用Google Gson。 我有: 我的课是: 问题答案: 这是执行此操作的简单代码,我避免了所有检查,但这是主要思想。 为了使使用更加通用- 您会发现Gson的javadocs非常清楚并且很有帮助。

  • 问题内容: 我正在解析我的代码。但是在检索已解析的数据时遇到了一些意外的问题。所以,让我解释一下我的问题。 我必须使用xcode 解析以下数据。这是我在浏览器中命中相同URL时要解析的数据的样子: 我要获取此数据的代码如下: 如果我使用来打印对象,即它看起来像: 因此,在这里我观察到两件事: 键(或节点)的序列(,和)被改变相比上述网络响应为。 将被括在大括号中。 现在,如果我将键&和 它们的值分

  • 问题内容: 我想使用Api GSON通过java解析JSON文件以获取JSON文件的最后一个字段: 描述符.json: ListTeleServices.java: TeleService.java: Record.java: 最后是我的解析器类 JSONMainParse.java: 对我来说似乎正确,它应该显示:“ amine1”,但它在以下位置给了我一个 nullPointerExcepti

  • 问题内容: 如何使用java进行json数据解析?有哪些方法? 问题答案: 使用org.json 使用GSON

  • 问题内容: 假设我正在使用Google的Gson库将JSON解析为Java数据结构。 如果Java字段没有对应的JSON,是否有一种简单的引发异常的方法?也就是说,我希望要求JSON具有Java结构中的所有字段。 问题答案: Gson没有JSON模式验证功能来指定必须存在一个特定的元素,也没有办法指定必须填充Java成员。拥有这样的功能(例如带有注释)可能会很好。转到“ Gson问题列表” 并提出

  • 我可以在中解析多级嵌套结构吗,就像我们在Java和库中所做的那样? 我已经对此进行了探索。 在Apex Salesforce中解析JSON 但是我想要一个通用的解决方案,它可以将任何JSON解析为所需的Apex对象。