2016-12-14 71 views
0

我正在开发一个Prolog项目,可以从omdb api中解答关于电影和系列的问题。如何从Prolog中的Json字符串中的字段获取值?

我用这个用或电影的信息系列返回完整的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). 

如果我搜索例如在“神奇动物”,写(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/[email protected]_V1_SX300.jpg", 
"Metascore":"66", 
"imdbRating":"7.9", 
"imdbVotes":"75,816bID":"tt3183660", 
"Type":"movie", 
"Response":"True"} 

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

回答

0

我找到了解决方案。

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). 
相关问题