如何使用 ArduinoJson 从同一个文件中检索多个 NDJSON 对象?

How to retrieve multiple NDJSON objects from the same file using ArduinoJson?

我正在尝试使用 ArduinoJson 来解析 Google 的 quickdraw 数据集,其中包含 .ndjson 文件,其中包含多个对象。我想出了如何使用以下简单代码检索文件中的第一个对象:

DeserializationError deserialization_error = ArduinoJson::deserializeJson(doc, as_cstr);
if (deserialization_error) {
    printf("deserializeJson() failed: %s\n", deserialization_error.c_str());
}

但是,这只会解析 ndjson 文件中的第一个对象。

根据 website,我觉得其他事情应该自动发生:

NDJSON, JSON Lines
When parsing a JSON document from an input stream, ArduinoJson stops reading as soon as the document ends (e.g., at the closing brace).

This feature allows to read JSON documents one after the other; for example, it allows to read line-delimited formats like NDJSON or JSON Lines.

{"event":"add_to_cart"}
{"event":"purchase"}

有什么方法可以得到解析对象的字节长度,以便我可以继续使用cstring来解析连续的对象?我确实打印出了 cstring,它确实包含了整个 ndjson 文件。

我找到了。

多次调用即可:

  DeserializationError error = deserializeJson(doc, sceneFile);

或:

  deserializeJson(docline1, sceneFile);

  deserializeJson(docline2, sceneFile);

  deserializeJson(docline3, sceneFile);