从Prolog中的JSON对象获取值

从Prolog中的JSON对象获取值,json,prolog,swi-prolog,Json,Prolog,Swi Prolog,我有两个问题 如何在Prolog中访问JSON数据结构中的值 如何从谓词生成一个解决方案列表作为JSON 我的代码: handle(Request) :- format(user_output,"I'm here~n",[]), http_read_json(Request, DictIn,[json_object(term)]), %beat(DictIn.name,DictIn.move,X), %get list of solution of

我有两个问题

  • 如何在Prolog中访问JSON数据结构中的值
  • 如何从谓词生成一个解决方案列表作为JSON
我的代码:

handle(Request) :-
   format(user_output,"I'm here~n",[]),
   http_read_json(Request, DictIn,[json_object(term)]),
   %beat(DictIn.name,DictIn.move,X),
   %get list of solution of beat in to JSON and keep it in DictOut
   reply_json(DictOut).

我假设您使用的是SWI Prolog。您的代码建议您将JSON转换为SWI Prolog DICT。所以你需要改变

http\u read\u json(请求、措辞、[json\u对象(术语)])

http\u read\u json(请求,DictIn,[json\u对象(dict)])

或者您可以使用http\u read\u json\u dict/2。请注意,
Request
必须是PUT或POST请求,否则这些谓词将抛出
domain\u错误(类型、术语)

您可以打印出
DictIn
,查看要提取的字段

对于响应,请使用
reply\u json\u dict/{1,2}