我正在尝试编写一个谓词来读取和编写prolog中的JSON文件。
当我要求使用我自己的谓词对查询进行prolog时,它会回复此错误消息source_sink foo.json does not exists (no such file or directory)
。
我不知道为什么,因为foo.json
存在并且在目录中。
有任何想法或建议吗?
编辑1:
json_load(FileName, JSON) :- open(FileName, read, Str),
read_json(Str, X),
close(Str),
json_to_term(X, JSON).
read_json(Stream, []) :- at_end_of_stream(Stream).
read_json(Stream, [X|L]) :-
\+ at_end_of_stream(Stream),
read(Stream, X),
read_json(Stream, L).
答案 0 :(得分:0)
交换条款read_json/2
,因为您将自由变量X
与[]
统一,就好像json-file为空。
read_json(Stream, [X|L]) :-
\+ at_end_of_stream(Stream),
read(Stream, X),
read_json(Stream, L).
read_json(Stream, []) :- at_end_of_stream(Stream).