简体   繁体   中英

Generate predicates from Json

I have a WEB API done in Asp.Net that returns some points of interest in JSON. In prolog I obtain it by doing this:

poi(X):- http_client:http_get('http://localhost:XXXXX/api/PontosInteresse',X,[]).

This returns something of the sort:

X = '[{"ID":1,"nome":"teste","descricao":"gfds","tempoEstimado":21,"acessibilidade":1,"localID":1,"local":{"ID":1,"nome":"porto","latitude":"21","longitude":"-21","ApplicationUserID":null},"categoriaID":1,"categoria":{"ID":1,"nome":"cultura","ApplicationUserID":null},"hashtags":null,"ApplicationUserID":null},{"ID":2,"nome":"teste2","descricao":"qweqwe","tempoEstimado":13,"acessibilidade":1,"localID":1,"local":{"ID":1,"nome":"porto","latitude":"21","longitude":"-21","ApplicationUserID":null},"categoriaID":1,"categoria":{"ID":1,"nome":"cultura","ApplicationUserID":null},"hashtags":null,"ApplicationUserID":null},{"ID":3,"nome":"teste3","descricao":"fgdfg","tempoEstimado":0,"acessibilidade":0,"localID":1,"local":{"ID":1,"nome":"porto","latitude":"21","longitude":"-21","ApplicationUserID":null},"categoriaID":1,"categoria":{"ID":1,"nome":"cultura","ApplicationUserID":null},"hashtags":null,"ApplicationUserID":null}]'.

How can I transform this to a bunch of predicates, in the form of:

poi(ID, Nome, Descricao, TempoEstimado, Acessibilidade, LocalId).
local(ID, Nome, Latitude, Longitude).

an example of how to access untyped content:

:- use_module(library(http/json)).

tj :-   X = '[{"ID":1,"nome":"teste","descricao":"gfds","tempoEstimado":21,"acessibilidade":1,"localID":1,"local":{"ID":1,"nome":"porto","latitude":"21","longitude":"-21","ApplicationUserID":null},"categoriaID":1,"categoria":{"ID":1,"nome":"cultura","ApplicationUserID":null},"hashtags":null,"ApplicationUserID":null},{"ID":2,"nome":"teste2","descricao":"qweqwe","tempoEstimado":13,"acessibilidade":1,"localID":1,"local":{"ID":1,"nome":"porto","latitude":"21","longitude":"-21","ApplicationUserID":null},"categoriaID":1,"categoria":{"ID":1,"nome":"cultura","ApplicationUserID":null},"hashtags":null,"ApplicationUserID":null},{"ID":3,"nome":"teste3","descricao":"fgdfg","tempoEstimado":0,"acessibilidade":0,"localID":1,"local":{"ID":1,"nome":"porto","latitude":"21","longitude":"-21","ApplicationUserID":null},"categoriaID":1,"categoria":{"ID":1,"nome":"cultura","ApplicationUserID":null},"hashtags":null,"ApplicationUserID":null}]',
    open_any(string(X), read, S, Close, []),
    json_read(S, T),
    Close,
    maplist(convert, T, C),
    maplist(writeln, C).

convert(json(L),
    poi(ID, Nome, Descricao, TempoEstimado, Acessibilidade, LocalId)
    / local(IDLoc, NomeLoc, Latitude, Longitude)
) :-
    maplist({L}/[F,V]>>memberchk(F=V, L),
        ['ID', nome, descricao, tempoEstimado, acessibilidade, localID],
        [ID, Nome, Descricao, TempoEstimado, Acessibilidade, LocalId]),
    memberchk(local=json(LL), L),
    maplist({LL}/[F,V]>>memberchk(F=V, LL),
        ['ID', nome, latitude, longitude],
        [IDLoc, NomeLoc, Latitude, Longitude]).

that yields

poi(1,teste,gfds,21,1,1)/local(1,porto,21,-21)
poi(2,teste2,qweqwe,13,1,1)/local(1,porto,21,-21)
poi(3,teste3,fgdfg,0,0,1)/local(1,porto,21,-21)

edit

here is the equivalent code that avoids the powerful library(yall)

convert(json(L),
    poi(ID, Nome, Descricao, TempoEstimado, Acessibilidade, LocalId)
    / local(IDLoc, NomeLoc, Latitude, Longitude)
) :-
    maplist(field_value(L),
        ['ID', nome, descricao, tempoEstimado, acessibilidade, localID],
        [ID, Nome, Descricao, TempoEstimado, Acessibilidade, LocalId]),
    memberchk(local=json(LL), L),
    maplist(field_value(LL),
        ['ID', nome, latitude, longitude],
        [IDLoc, NomeLoc, Latitude, Longitude]).

field_value(Fields, Field, Value) :-
    memberchk(Field=Value, Fields).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM