从 Prolog 中的 JSON 对象获取值

Get value from JSON object in Prolog

我有两个问题。

我的代码:

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 指令。所以你需要改变

http_read_json(Request, DictIn, [json_object(term)])

http_read_json(Request, DictIn, [json_object(dict)])

或者您可以只使用 http_read_json_dict/2。请注意 Request 必须是 PUT 或 POST 请求,否则这些谓词将抛出 domain_error(Type, Term).

你可以打印出来DictIn看看你要提取哪些字段

响应使用 reply_json_dict/{1, 2}.