使用pandoc将html转换为json

时间:2022-07-03 20:29:52

I'm trying to take html and generate some json that keeps the same structure.

我正在尝试使用html并生成一些保持相同结构的json。

I'm trying to use pandoc, as i've had some success in transforming things from format A to format B using pandoc before.

我正在尝试使用pandoc,因为我在使用pandoc之前将事物从格式A转换为格式B方面取得了一些成功。

I'm trying to convert this file:

我正在尝试转换此文件:

example.html

<p>Hello guys! What's up?</p>

Using the command:

使用命令:

pandoc -f html -t json example.html

What i expect is something like:

我期望的是:

[{ "p": "Hello guys! What's up?"}]

What i get is:

我得到的是:

[
  { "Para":
    [
      {"t": "Str", "c": "Hello"},
      {"t": "Space"},
      {"t": "Str", "c": "guys!"},
      {"t": "Space"},
      {"t": "Str", "c": "What's"},
      {"t": "Space"},
      {"t": "Str", "c": "up?"}
    ]
  }
]

The problem seems to be that when pandoc reads the text content, it separates every word based on the space character and makes an array out of it, while i expected pandoc to understand that the whole string is a single element.

问题似乎是当pandoc读取文本内容时,它会根据空格字符分隔每个单词并从中生成一个数组,而我希望pandoc能够理解整个字符串是单个元素。

I'm a beginner at pandoc and I've not been able to find out how to tweak that behavior.

我是pandoc的初学者,我无法找到如何调整这种行为。

Do you have an idea of how I can get the desired output? Do you know another tool that can do this? The tool, or the language it's written in doesn't matter.

你知道我如何获得所需的输出吗?你知道另一种可以做到这一点的工具吗?该工具或其编写的语言无关紧要。

Thanks.

Edit: You can test that behavior online on that pandoc online tool.

编辑:您可以在该pandoc在线工具上在线测试该行为。

Edit 2: Workaround. I couldn't find how to do the HTML->JSON conversion with pandoc. As a workaround, i used the suggestion proposed in the comments, and implemented a solution using Himalaya, which is a node package. The result is exactly what i wished for, even though it's not using pandoc.

编辑2:解决方法。我找不到如何使用pandoc进行HTML-> JSON转换。作为一种解决方法,我使用了评论中提出的建议,并使用喜马拉雅实现了一个解决方案,这是一个节点包。结果正是我所希望的,即使它没有使用pandoc。

1 个解决方案

#1


1  

Currently, the pandoc JSON representation is not very human-readable, but is auto-generated from the Haskell pandoc data types (aka document AST). There is some discussion to change that eventually.

目前,pandoc JSON表示不是非常易读的,而是从Haskell pandoc数据类型(也就是文档AST)自动生成的。有一些讨论要最终改变。

I guess you're looking for something like https://codebeautify.org/xmltojson? There also seem to be plenty of commandline-tools that do that.

我想你正在寻找像https://codebeautify.org/xmltojson这样的东西?似乎还有很多命令行工具可以做到这一点。

#1


1  

Currently, the pandoc JSON representation is not very human-readable, but is auto-generated from the Haskell pandoc data types (aka document AST). There is some discussion to change that eventually.

目前,pandoc JSON表示不是非常易读的,而是从Haskell pandoc数据类型(也就是文档AST)自动生成的。有一些讨论要最终改变。

I guess you're looking for something like https://codebeautify.org/xmltojson? There also seem to be plenty of commandline-tools that do that.

我想你正在寻找像https://codebeautify.org/xmltojson这样的东西?似乎还有很多命令行工具可以做到这一点。