问题
In order to build a JSONArray
, how is data from BaseX
exported as JSON
? Or queried with XQuery
so that the String
can be read with Java. Output as:
[
{
"0":"z10",
"1":"y9",
"2":"x7",
"3":"atrib6",
"name":"alice"
},
{
"0":"home5",
"1":"cell4",
"name":"sue"
},
{
"0":"phone3",
"1":"phone2",
"2":"phone1",
"name":"joe"
},
{
"name":"people"
}
]
How would loadPeople
perform the inverse operation of addPeople
? There must be an input of String
to addPeople
. Where addPeople
takes a JSONArray
and persists it with BaseX
, loadPeople grabs a string.
The desired output of loadPeople
would be that original JSONArray
array retrieve from BaseX
.
code:
package groupBaseX.basex.json;
import groupBaseX.io.Person;
import java.util.Properties;
import java.util.logging.Logger;
import org.basex.core.BaseXException;
import org.basex.core.Command;
import org.basex.core.Context;
import org.basex.core.cmd.Add;
import org.basex.core.cmd.List;
import org.basex.core.cmd.Open;
import org.basex.core.cmd.Set;
import org.basex.io.in.ArrayInput;
import org.json.JSONArray;
import org.json.JSONObject;
public class DatabaseHelper {
private static final Logger log = Logger.getLogger(DatabaseHelper.class.getName());
private Properties properties = new Properties();
private String databaseName = null;
private Context context = null;
private String parserType = null;
private DatabaseHelper() {
}
public DatabaseHelper(Properties properties) {
this.properties = properties;
}
private void init() throws BaseXException {
log.fine(properties.toString());
parserType = properties.getProperty("parserType");
databaseName = properties.getProperty("databaseName");
context = new Context();
log.fine(new List().execute(context));
}
public void loadPeople() throws BaseXException {
init();
log.fine(new Open(databaseName).execute(context));
log.fine(new Set("parser", "json").execute(context));
//
//should return JSONArray
}
public void addPeople(java.util.List<Person> people) throws BaseXException {
init();
log.fine(new Open(databaseName).execute(context));
log.fine(new Set("parser", "json").execute(context));
JSONObject jsonPerson = null;
JSONArray jsonPeople = new JSONArray();
Command add = null;
for (Person person : people) {
jsonPerson = new JsonHelper(person).convert();
jsonPeople.put(jsonPerson);
}
add = new Add(".json");
add.setInput(new ArrayInput(jsonPeople.toString()));
log.fine(add.execute(context));
}
}
Perhaps it's a question of using XQuery
to return the JSONArray
as a String
? But, that seems awkward given that the database itself has the capability of simply exporting the data.
来源:https://stackoverflow.com/questions/60268157/how-to-export-json-from-basex-with-java