简体   繁体   English

如何在Haskell中解析exiftool JSON输出的示例

[英]Example of how to parse exiftool JSON output in Haskell

I can't make sense of any of the documentation. 我无法理解任何文档。 Can someone please provide an example of how I can parse the following shortened exiftool output using the Haskell module Text.JSON ? 有人可以提供一个示例,说明如何使用Haskell模块Text.JSON解析以下缩短的exiftool输出? The data is generating using the command exiftool -G -j <files.jpg> . 使用命令exiftool -G -j <files.jpg>生成数据。

[{
  "SourceFile": "DSC00690.JPG",
  "ExifTool:ExifToolVersion": 7.82,
  "File:FileName": "DSC00690.JPG",
  "Composite:LightValue": 11.6
},
{
  "SourceFile": "DSC00693.JPG",
  "ExifTool:ExifToolVersion": 7.82,
  "File:FileName": "DSC00693.JPG",
  "EXIF:Compression": "JPEG (old-style)",
  "EXIF:ThumbnailLength": 4817,
  "Composite:LightValue": 13.0
},
{
  "SourceFile": "DSC00694.JPG",
  "ExifTool:ExifToolVersion": 7.82,
  "File:FileName": "DSC00694.JPG",
  "Composite:LightValue": 3.7
}]

Well, the easiest way is to get back a JSValue from the json package, like so (assuming your data is in text.json): 好吧,最简单的方法是从json包中获取JSValue,就像这样(假设你的数据在text.json中):

Prelude Text.JSON> s <- readFile "test.json"
Prelude Text.JSON> decode s :: Result JSValue
Ok (JSArray [JSObject (JSONObject {fromJSObject = [("SourceFile",JSString (JSONString {fromJSString = "DSC00690.JPG"})),("ExifTool:ExifToolVersion",JSRational False (391 % 50)),("File:FileName",JSString (JSONString {fromJSString = "DSC00690.JPG"})),("Composite:LightValue",JSRational False (58 % 5))]}),JSObject (JSONObject {fromJSObject = [("SourceFile",JSString (JSONString {fromJSString = "DSC00693.JPG"})),("ExifTool:ExifToolVersion",JSRational False (391 % 50)),("File:FileName",JSString (JSONString {fromJSString = "DSC00693.JPG"})),("EXIF:Compression",JSString (JSONString {fromJSString = "JPEG (old-style)"})),("EXIF:ThumbnailLength",JSRational False (4817 % 1)),("Composite:LightValue",JSRational False (13 % 1))]}),JSObject (JSONObject {fromJSObject = [("SourceFile",JSString (JSONString {fromJSString = "DSC00694.JPG"})),("ExifTool:ExifToolVersion",JSRational False (391 % 50)),("File:FileName",JSString (JSONString {fromJSString = "DSC00694.JPG"})),("Composite:LightValue",JSRational False (37 % 10))]})])

this just gives you a generic json Haskell data type. 这只是为您提供了一个通用的json Haskell数据类型。

The next step will be to define a custom Haskell data type for your data, and write an instance of JSON for that, that converts between JSValue's as above, and your type. 下一步是为您的数据定义一个自定义Haskell数据类型,并为其编写一个JSON实例,它在上面的JSValue和您的类型之间进行转换。

Thanks to all. 谢谢大家。 From your suggestions I was able to put together the following which translates the JSON back into name-value pairs. 根据您的建议,我能够将以下内容放在一起,将JSON转换回名称 - 值对。

data Exif = 
    Exif [(String, String)]
    deriving (Eq, Ord, Show)

instance JSON Exif where
    showJSON (Exif xs) = showJSONs xs
    readJSON (JSObject obj) = Ok $ Exif [(n, s v) | (n, JSString v) <- o]
        where 
            o = fromJSObject obj
            s = fromJSString

Unfortunately, it seems the library is unable to translate the JSON straight back into a simple Haskell data structure. 不幸的是,似乎库无法将JSON直接转换回简单的Haskell数据结构。 In Python, it is a one-liner: json.loads(s) . 在Python中,它是一个单行: json.loads(s)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM