如何从Prolog的Json字符串中的字段中获取值? [英] How to get a value from a field in a Json string in Prolog?

查看:40
本文介绍了如何从Prolog的Json字符串中的字段中获取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事一个 Prolog 项目,该项目可以通过 omdb api 回答有关电影和连续剧的问题.

I am working on a Prolog project that can answer questions about Movies and Series from the omdb api.

我用它来返回包含电影或系列信息的完整 Json 字符串:

I use this to return the complete Json string with information of a Movie or Series:

findMovie(X,Json):-
    atomic_list_concat(X, ',', Atom),
    uri_query_components(QS, [t=Atom]) %t is the title of the movie
    format(atom(HREF),'http://www.omdbapi.com/?~s',[QS]),
    http_get(HREF,Json, []),
    write(Json).

如果我搜索例如:神奇动物",write(Json) 将在控制台中打印以下内容:

If I search for example on: "Fantastic Beasts", write(Json) will print the following in the console:

{"Title":"Fantastic Beasts and Where to Find Them",
"Year":"2016",
"Rated":"PG-13",
"Released":" 2016",
"Runtime":"133 min",
"Genre":"Adventure, Family, Fantasy",
"Director":"David Yates","WriJ.K. Rowling",
"Actors":"Eddie Redmayne, Sam Redford, Scott Goldman, Tim Bentinck",
"Plot":"Thetures of writer Newt Scamander in New York's secret community of witches and wizards seventy before Harry Potter reads his book in school.",
"Language":"English",
"Country":"UK, USA","Awards":"1 nomination.",
"Poster":"https://images-na.ssl-images-amazon.com/images/M/MV5BMjMxOTM1OTI4MV5BBnXkFtZTgwODE5OTYxMDI@._V1_SX300.jpg",
"Metascore":"66",
"imdbRating":"7.9",
"imdbVotes":"75,816bID":"tt3183660",
"Type":"movie",
"Response":"True"}

如何返回值?例如:Year"的值是 2016.我已经阅读了一些关于将 Json 字符串转换为 Prolog 格式的内容,但我无法弄清楚.

How can I return a value? For example: the value of "Year" which is 2016. I've read some things about converting the Json string to a Prolog format but I couldn't figure it out.

推荐答案

我找到了解决方案.

findMovie(X,Json):-
    Field = 'Year',
    atomic_list_concat(X, ',', Atom),
    uri_query_components(QS, [t=Atom]) %t is the title of the movie
    format(atom(HREF),'http://www.omdbapi.com/?~s',[QS]),
    http_get(HREF,json(Json), []),   %json(Json) converts it to Prolog terms.
    member(Field=Result,Json),    %Result will get the value of 'Year'
    write(Result).

这篇关于如何从Prolog的Json字符串中的字段中获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆