Retrieving json query from postgres and display in java -
string selecttablesql = "select user_id, username dbuser"; statement statement = dbconnection.createstatement(); resultset rs = statement.executequery(selecttablesql); while (rs.next()) { string userid = rs.getstring("user_id"); string username = rs.getstring("username"); }
with above code..i able establish connection postgres , display values database table. how parse json select query -
select array_to_json(array_agg(row_to_json(t))) ( select user_id, username dbuser ) t
the resultant json structure above select query in postgres -
[{user_id:"123",username:"abc"},{user_id:"234",username:"pqr"}]
not understanding how read above json "select" query postgres , display values "user_id" , "username" separately in java.
i beginner.any appreciated.thankyou in advance.
you should use jackson - objectmapper class this
objectmapper mapper = new objectmapper(); list<user> userlist = mapper.readvalue(jsonstring, typefactory.constructcollectiontype(list.class, user.class)); for(user val : userlist){ system.out.println("user id : "+val.getuserid); system.out.println("user id : "+val.getusername); }
it work fine
list<user> userlist= arrays.aslist(mapper.readvalue(json, user[].class));
Comments
Post a Comment