in JAVA - How to insert json data into a nested json property? in JAVA - How to insert json data into a nested json property? json json

in JAVA - How to insert json data into a nested json property?


Your classes will have to be structured like this:

class User{    public string fname;    public string lname;    public string address;}class ResultType{    public int id;    public string dest_country;    public User user;}

This should generate the results you are looking for when parsed to JSON.The json is based on the current class structure so the user that you want to be in a nested property has to be generated from a prooperty which is an object containing that data.

Another way would be to reconstruct your json data afterwards but that's not as tidy. Like so:

result.each(function(){    this.user = { fname: this.fname, lname: this.lname, address: this.address };});


Use the javax.json.JsonObject, try the following code:

JSONArray resultJson = new JSONArray();try{ConnectToDB();stmt = conn.prepareStatement(query);m_ResultSet = stmt.executeQuery();while (m_ResultSet.next()) {  JsonObject userObject= Json.createObjectBuilder()    .add("fname", m_ResultSet.getString("fname")    .add("lname", m_ResultSet.getString("lname")    .add("adress", m_ResultSet.getString("adress").build(); JsonObject resultJson = Json.createObjectBuilder()  .add("id", m_ResultSet.getInt("id"))  .add("dest_country", m_ResultSet.getString("dest_country"))  .add("user", userObject).build();}

That should do it.