Как разобрать результаты XQuery в объект? - PullRequest
0 голосов
/ 16 февраля 2020

Результат из приведенного ниже XQuery не совсем JSON, хотя он возник как таковой - теперь он XML из BaseX. Как я могу построить List из Person объектов?

вывод:

thufir@dur:~/NetBeansProjects/groupBaseX$ 
thufir@dur:~/NetBeansProjects/groupBaseX$ gradle run

> Task :run FAILED
Feb. 16, 2020 12:09:36 P.M. basex.DatabaseQueries iterate
INFO: <json type="array">
  <_ type="object">
    <_0030/>
    <_0031>z10</_0031>
    <_0032>y9</_0032>
    <_0033>x7</_0033>
    <_0034>atrib6</_0034>
    <name>alice</name>
  </_>
  <_ type="object">
    <_0030>home5</_0030>
    <_0031>cell4</_0031>
    <name>sue</name>
  </_>
  <_ type="object">
    <_0030>phone3</_0030>
    <_0031>phone2</_0031>
    <_0032>phone1</_0032>
    <name>joe</name>
  </_>
  <_ type="object">
    <name>people</name>
  </_>
</json>
Exception in thread "main" org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]
        at org.json.JSONTokener.syntaxError(JSONTokener.java:507)
        at org.json.JSONArray.<init>(JSONArray.java:109)
        at org.json.JSONArray.<init>(JSONArray.java:162)
        at basex.DatabaseQueries.iterate(DatabaseQueries.java:78)
        at groupBaseX.App.run(App.java:21)
        at groupBaseX.App.main(App.java:37)

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':run'.
> Process 'command '/home/thufir/.sdkman/candidates/java/12.0.1-zulu/bin/java'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s
3 actionable tasks: 1 executed, 2 up-to-date
thufir@dur:~/NetBeansProjects/groupBaseX$                                                       

Класс Person:

package basex;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

public class Person {

    private static final Logger log = Logger.getLogger(Person.class.getName());
    private String name = "";
    private List<String> attributes = new ArrayList<>();

    private Person() {
    }

    Person(String name, List<String> attributes) {
        this.name = name;
        this.attributes = attributes;
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        for (String s : attributes) {
            stringBuilder.append(s);
            stringBuilder.append("\t");
        }
        return "\n\n\n" + name + "\n" + stringBuilder.toString();
    }

    public String getName() {
        return name;
    }

    public List<String> getAttributes() {
        return attributes;
    }
}

запрос к базе данных:

    public void iterate() throws BaseXException {
        init();
        new Open(databaseName).execute(context);
        String result = new XQuery(".").execute(context); //root
        log.info(result);

        JSONArray people = new JSONArray(result);
        //    JSONObject person = null;
        //   for (int i = 0; i < people.length(); i++) {
        //        person = people.getJSONObject(i);

        //     }
    }
...