使用 Jackson 读取单个 JSON 事件

发布于 2024-11-04 07:10:45 字数 656 浏览 1 评论 0原文

我有一个包含多个 JSON 对象的文件,每个对象都可能包含其他对象和数组。

我想将其反序列化,最好使用 Jackson 库,将其反序列化为维护顶级对象分离的数据结构,例如 Java HashMap 对象的数组或列表,其中每个HashMap 将包含文件中单个顶级 JSON 对象的数据。

据我所知,您无法从 Jackson 获得 HashMap,因此我必须忍受 HashMap

List<HashMap<String,Object>> values = new ObjectMapper().readValue(new BufferedReader(new FileReader(path)), new TypeReference<List<HashMap<String, Object>>>() {});

上面的反序列化按预期工作,但是我得到了所有文件数据,而不是单个 JSON 对象的数据,如我所愿。

有什么想法吗?

谢谢!

I have a file with several JSON objects, each of which may contain other objects and arrays.

I would like to deserialize this, preferably using the Jackson library, into a data structure that maintains top-level object separation, e.g. an array or a list of Java HashMap<String, String> objects, where each HashMap will contain the data of a single top-level JSON object from the file.

From what I have seen, you can't get a HashMap<String, String> from Jackson, so I have to put up with a HashMap<String, Object>:

List<HashMap<String,Object>> values = new ObjectMapper().readValue(new BufferedReader(new FileReader(path)), new TypeReference<List<HashMap<String, Object>>>() {});

The deserialization above works as expected, however I get all the file data and a not a single JSON object's data, as I would like.

Any ideas?

Thanks!

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

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

发布评论

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

评论(1

踏月而来 2024-11-11 07:10:45

经过相当多的努力,这里有一个似乎可行的简单方法:

Reader inputReader = new BufferedReader(new FileReader(filename));

int readEvents = 0;

JsonFactory factory = new JsonFactory();

JsonParser parser = factory.createJsonParser(inputReader);

JsonToken token = null;
ObjectMapper mapper = new ObjectMapper();
HashMap<String,Object> attributes = null;
ArrayList<Map<String,Object>> matchedEvents = new ArrayList<Map<String,Object>>();

try
{
    while ((token=parser.nextToken()) != null)
    {
        if (token == JsonToken.START_OBJECT)
        {
            readEvents++;
            attributes = mapper.readValue(parser,
                     new TypeReference<HashMap<String, Object>>() {});
            if (attributes != null)
            {
                matchedEvents.add(attributes);
            }
        }
    }
}
catch (Exception e)
{
    e.printStackTrace();
}

System.out.println("Read " + readEvents + " events");

上面的 ArrayList 中放置的每个 HashMap 对象都对应于一个单独的对象。 (顶级)JSON 事件。

readValue() 方法显然不会返回 Map(String, String) 对象。

After quite a bit of effort, here is a simple approach that seems to work:

Reader inputReader = new BufferedReader(new FileReader(filename));

int readEvents = 0;

JsonFactory factory = new JsonFactory();

JsonParser parser = factory.createJsonParser(inputReader);

JsonToken token = null;
ObjectMapper mapper = new ObjectMapper();
HashMap<String,Object> attributes = null;
ArrayList<Map<String,Object>> matchedEvents = new ArrayList<Map<String,Object>>();

try
{
    while ((token=parser.nextToken()) != null)
    {
        if (token == JsonToken.START_OBJECT)
        {
            readEvents++;
            attributes = mapper.readValue(parser,
                     new TypeReference<HashMap<String, Object>>() {});
            if (attributes != null)
            {
                matchedEvents.add(attributes);
            }
        }
    }
}
catch (Exception e)
{
    e.printStackTrace();
}

System.out.println("Read " + readEvents + " events");

Each of the HashMap<String, Object> objects placed in the above ArrayList corresponds to a single (top-level) JSON event.

The readValue() methods apparently do not return a Map(String, String) object.

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