读取json文件并解析灵药和毒药

问题描述 投票:0回答:1

尝试使用

File.read()
读取文件,然后将其通过管道传输到
Poison.decode!()
,但当我尝试运行它时,我不断收到此错误。

1st argument: not an iodata term

:erlang.iolist_to_binary(:enoent)
(poison 5.0.0) lib/poison/parser.ex:103: Poison.Parser.parse!/2

解析json文件的函数

defmodule PROCESS_JSON do
  @json_path "./data.json"

  def parse_json() do
    @json_path
    |> File.read()
    // |> elem(1) Tried with and without this but get same error. 
    |> Poison.decode!()
  end
end
elixir
1个回答
0
投票

我建议您使用

File.read!
(与
!
)代替,因为这样可以代替:

{:error, :enoent}

你会得到这个:

** (File.Error) could not read file "./data.json": no such file or directory

这更清楚了。

正如丹尼尔在评论中所说,问题是该文件不存在。

仅供参考 Jason 是新项目 JSON 解析的流行选择。

© www.soinside.com 2019 - 2024. All rights reserved.