2016-12-14 14 views
0

私はomdb apiからMoviesとSeriesに関する質問に答えることができるPrologプロジェクトに取り組んでいます。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"} 

どのように値を返すことができますか?たとえば、 "Year"の値は2016です.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). 
関連する問題