Вывод нескольких объектов в один файл JSON, приводящий к ОШИБКЕ: стандарт JSON допускает только на одно значение верхнего уровня меньше - PullRequest
0 голосов
/ 31 октября 2019

В приложении чата я пытаюсь написать два объекта: 1. Имя разговора, за которым следует новая строка, и 2. Сообщения разговора (состоящие из времени сообщения, имени пользователя и содержимого сообщения).

<conversation_name><new_line>
(<unix_timestamp><space><username><space><message><new_line>)*

Мой метод ниже пытается сделать это в формате, указанном выше:

private void writeToJson(OutputStream outputStream, BufferedWriter bufferedWriter, Conversation conversationWithReport) throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Instant.class, new InstantSerializer());
    Gson g = gsonBuilder.create();
try {
    bufferedWriter.write(g.toJson(conversationWithReport.conversation_name));
    bufferedWriter.newLine();
    bufferedWriter.write(g.toJson(conversationWithReport.messages));
    bufferedWriter.write(g.toJson(conversationWithReport.user_activity_report));
} catch (IOException e) {
    System.out.println("There was an issue in outputting the conversation into its written JSON format.");
}
bufferedWriter.close();
outputStream.close();
}

Однако я сталкиваюсь с вопросом:

ERROR: JSON standard allows only one top-level value less... 

Как вывод:

"My Conversation"
[{"unix_timestamp":1448470901,"username":"bob","message":"Hello there!"},{"unix_timestamp":1448470905,"username":"mike","message":"how are you?"},{"unix_timestamp":1448470906,"username":"bob","message":"I\u0027m good thanks, do you like pie?"},{"unix_timestamp":1448470910,"username":"mike","message":"no, let me ask Angus..."},{"unix_timestamp":1448470912,"username":"angus","message":"Hell yes! Are we buying some pie?"},{"unix_timestamp":1448470914,"username":"bob","message":"No, just want to know if there\u0027s anybody else in the pie society..."},{"unix_timestamp":1448470915,"username":"angus","message":"YES! I\u0027m the head pie eater there..."},{"unix_timestamp":1448470919,"username":"dave","message":"Ok, here\u0027s my card number: 4321567890121234. What\u0027s your phone number bob? I\u0027ll send you the adress of the pie shop..."},{"unix_timestamp":1448470919,"username":"bob","message":"it\u0027s 07804377261. Thanks, looking forward to the pie!"}]["This is the report for the most active users, in orderof the number of messages they sent: ","bob\u003d4","mike\u003d2","angus\u003d2","dave\u003d1"]

Как получить выходного сына под одним верхним уровнем в соответствии с условиями ошибки?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...