简体   繁体   English

如何在谓词中使用序言字典的键值?

[英]How to use the key values of a prolog dict in a predicate?

I have a simple question which I'm getting a lot of trouble finding an answer. 我有一个简单的问题,很难找到答案。

I'm building an api that reads json and at the moment it successfully converts the json to a dict with http_read_json_dict. 我正在构建一个读取json的api,目前它已成功通过http_read_json_dict将json转换为dict。 The problem is that, I have no clue how to access the attributes inside the dict and use that that data in a predicate. 问题是,我不知道如何访问字典中的属性并在谓词中使用该数据。
I tried following the swi-prolog documentation but to no avail... Do you guys know of a way? 我尝试遵循swi-prolog文档,但无济于事...你们知道一种方法吗?

I tried this to test read the content of the dict but I get a "key_value expected, found a dict" error. 我试图通过此测试来读取dict的内容,但是却收到"key_value expected, found a dict"错误。 Please don't burn me at the stake: 请不要把我烧死在火刑柱上:

json contains: [{"task":"learnprolog","dayofmonth":2}] json包含: [{"task":"learnprolog","dayofmonth":2}]

  http_read_json_dict(Request, JSONIn,[json_object(term)]),
  X = JSONIn,
  format(user_output,"task is: ~p~n",[X.task]),
  JSONOut=JSONIn.

But i was really hoping to do something like: 但是我真的希望做这样的事情:

predicate(X):- X.dayofmonth==2,doSomething(X.task);doSomethingElse(X.task) . predicate(X):- X.dayofmonth==2,doSomething(X.task);doSomethingElse(X.task)

Thanks a lot for the help! 非常感谢您的帮助!

not sure about the details, without a server setup similar to what you're using... but 不知道细节,没有与您使用的服务器类似的服务器设置...但是

?- open_codes_stream(`[{"task":"learnprolog","dayofmonth":"2"}]`,S),json_read_dict(S,[J]),close(S),write(J.dayofmonth).
2
S = <stream>(0x7f5f14304dd0),
J = _G15591{dayofmonth:"2", task:"learnprolog"}.

note the square brackets around J 注意J周围的方括号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM