简体   繁体   English

如何在JSON中只获取大数组的第一个元素?

[英]How to get only the 1st element of an large array in JSON?

I have been looking at the Streaming implementation of the Gson Library. 我一直在关注Gson Library的Streaming实现。 But I cannot determine if it would be a good practice to do like the following to get the 1st element of an array. 但是我无法确定像下面那样获取数组的第一个元素是否是一个好习惯。 The URL returns an InputStream which contains a Json array. URL返回包含Json数组的InputStream

JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
reader.beginArray();
Message message = new gson().fromJson(reader, Message.class);
reader.endArray(); // this line
reader.close();

My question lies on the line that has the comment beside. 我的问题在于旁边有评论的界限。 Should I do that if I want only to get the first element? 如果我只想获得第一个元素,我应该这样做吗?

Note that here I want to know about the efficiency of the code. 请注意,我想知道代码的效率。 The code is directly copied from the gson streaming example [with some modification to describe the situation correctly]. 代码直接从gson流示例中复制[通过一些修改来正确描述情况]。

The actual array is very large. 实际的数组非常大。 That's why I cannot just wait to get the whole array and then get the element. 这就是为什么我不能等待获得整个数组然后获得元素。 So I want to break the process as soon as I get the first element and then close the stream. 所以我想在获得第一个元素然后关闭流后立即中断该过程。

Also please recommend me of any better approach if there are any. 如果有的话,请向我推荐任何更好的方法。

Note that here I want to know about the efficiency of the code 请注意,我想知道代码的效率

Well, then look at the code :) Source for JsonReader 那么,看看代码:) JsonReader的源代码

It does pretty much what you would expect. 它几乎可以满足您的期望。 It's using the provided InputStreamReader and only reading what it needs to fulfill an operation (in your case, getting the first object from the JSON array). 它使用提供的InputStreamReader ,只读取完成操作所需的内容(在您的情况下,从JSON数组中获取第一个对象)。

However ... when you call endArray() ... it's going to read from the stream until it finds the closing ] of that array. 但是...当你调用endArray() ......这是怎么回事,直到它找到收盘从流中读取]数组中。 You don't actually care about that, and certainly don't want to spend the time reading all that data from the wire. 你实际上并不关心这一点,当然也不想花时间从线上读取所有数据。 Just call close() . 只需调用close()

Since you're closing the JsonReader (and thus the underlying stream) ... that's about as good as you're going to get. 因为你正在关闭JsonReader (以及基础流)......这就像你将要获得的一样好。 You only read the data you wanted from the underlying stream then closed it. 您只能从基础流中读取所需数据,然后将其关闭。

Edit from comments: You can make your cases nice and tidy by doing: 从评论中编辑:您可以通过以下方式使您的案例变得干净整洁:

public Message getFirstMessageFromArray(JsonReader reader) {
    reader.beginArray();
    return getMessageFromJson(reader);
}

public Message getMessageFromJson(JsonReader reader) {
    return new gson().fromJson(reader, Message.class);
}

And when you wanted to use it: 当你想要使用它时:

JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
Message message = getFirstMessageFromArray(reader);
reader.close();

or 要么

JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
Message message = getMessageFromJson(reader);
reader.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM