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

会有一股神秘感。 提交于 2019-12-25 08:37:24

问题


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

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

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

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.


回答1:


I found the solution for this.

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


来源:https://stackoverflow.com/questions/41142552/how-to-get-a-value-from-a-field-in-a-json-string-in-prolog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!