如何从 Prolog 中的 Json 字符串中的字段获取值?

How to get a value from a field in a Json string in Prolog?

我正在做一个 Prolog 项目,它可以从 omdb api.

中回答有关电影和系列的问题

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

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).

如果我在 "Fantastic Beasts" 上搜索示例,write(Json) 将在控制台中打印以下内容:

{"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"}

我怎样才能return一个值?例如:"Year" 的值为 2016。我读过一些关于将 Json 字符串转换为 Prolog 格式的内容,但我无法弄明白。

我找到了解决方案。

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).