将单个值作为 JSON 流式传输到整个 JSON 映射中

发布于 2025-01-08 07:23:28 字数 680 浏览 4 评论 0原文

我有以下格式的 JSON

{ "a":"b" , "Content" :; , "x" : y" }

等等。FILEA

太大,我无法打开并将其加载到主内存。 是否有任何选项可以让我将这个 json 流式传输到 Web 服务,而无需使用 Java 占用大量主内存。

例如,如果有类似的东西那就太棒了 writer = new JsonWriter(new FileWriter("user.json"));

writer.beginObject(); // {
writer.name("name");
writer.value("messagesPart1"); // "messages" : 
    writer.flush();
    writer.value("messagesPart2"); // "messages" :
    writer.flush(); 
writer.value("messagesPart3"); // "messages" : 
writer.endObject();
    }

user.json fie 的内容是

{ "name" : "messagesPart1messagesPart2messagesPart3"}

I have a JSON of following format

{ "a":"b" , "Content" : <Content of file FILEA> , "x" : y" }

and so on.

FILEA is too big that i cant open and load it to main memory.
Is there any option where i can stream this json to a webservice without using much of main memory using Java.

For example , it would be awesome if there is something like
writer = new JsonWriter(new FileWriter("user.json"));

writer.beginObject(); // {
writer.name("name");
writer.value("messagesPart1"); // "messages" : 
    writer.flush();
    writer.value("messagesPart2"); // "messages" :
    writer.flush(); 
writer.value("messagesPart3"); // "messages" : 
writer.endObject();
    }

And the content of user.json fie is

{ "name" : "messagesPart1messagesPart2messagesPart3"}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丑疤怪 2025-01-15 07:23:28

我建议您使用 Jackson 库(Java 中最强大的 Json 库之一)。

它具有将 Json 流式传输到 OutpuStream 的功能(因此您可以有一个 Web 套接字或一个开放连接来输出内容...)。这是高级文档:生成器

还有一个使用此生成器的用例(Twittter),请检查此 post

但是 Jakson 快速入门也有一个很好的 简介

I recommend you to use the Jackson library (one of the most powerful for Json in Java).

It has the functionality to stream Json to OutpuStream (so you could have a web socket or an open connection to output the content...). Here is the high level doc: Generator.

There is also a use case (Twittter) that makes use of this generator, check this post

But the Jakson quick start has also a good introduction

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文