LoginSignup
0
0

More than 3 years have passed since last update.

SWI-Prolog で JSON ファイルを読む

Last updated at Posted at 2018-10-17
json_read.pl
/* ------------------------------------------------------------------- */
/*
    json_read.pl

                        Oct/18/2018
*/
/* ------------------------------------------------------------------- */
:- use_module([ library(http/json) ]).
:- initialization main.

/* ------------------------------------------------------------------- */
%% [4-6-8]:
display_line_proc(Dict,Key) :-
    write(current_output ,Key),
    write(current_output ,'\t'),
    write(current_output ,Dict.Key.name),
    write(current_output ,'\t'),
    write(current_output ,Dict.Key.population),
    write(current_output ,'\t'),
    writeln(current_output ,Dict.Key.date_mod).
/* ------------------------------------------------------------------- */
%% [4-6]:
print_list(Dict,[]).
print_list(Dict,[Element|List]) :-
    display_line_proc(Dict,Element),
    print_list(Dict,List).

/* ------------------------------------------------------------------- */
%% [4]:
reading_JSON_term(File_in) :-
    write(File_in),nl,
    open(File_in, read, Stream, [] ),
    json_read_dict(Stream, Dict, []),
    close(Stream),
%%
    dict_keys(Dict,Keys),
    length(Keys,Ll),
    writeln(current_output , Ll),
    print_list(Dict,Keys).

/* ------------------------------------------------------------------- */
main(Argv) :-
    format('*** 開始 ***\n'),
    format('Argv: ~w\n', [Argv]),
    [File_in|_] = Argv,
    reading_JSON_term(File_in),
    format('*** 終了 ***\n'),
    halt.

/* ------------------------------------------------------------------- */

実行方法

swipl -f ./json_read.pl /var/tmp/json/cities.json
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0