将单个值作为 JSON 流式传输到整个 JSON 映射中
我有以下格式的 JSON
{ "a":"b" , "Content" :
等等。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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您使用 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
此博客链接有一些有用的信息 - http://architectvm.blogspot.in/2012/02/stream-single-value-as-json-in-whole.html
This blog link have some usefull info on the same - http://architectvm.blogspot.in/2012/02/stream-single-value-as-json-in-whole.html