Java内置的JSON或XML数据解析器 [英] Java Built-in data parser for JSON or XML or else

查看:90
本文介绍了Java内置的JSON或XML数据解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取存储在文件中的数据。我还没有决定存储它的格式,但我正在寻找一种易于解析的格式。最初我以为我会使用JSON,但似乎Java没有内置的JSON解析器。

I want to read data that's stored in a file. I haven't decide yet what format to store it, but I'm looking for a format that's easy to parse. Initially I thought I'd go with JSON, but it seems Java doesn't have a built-in parser for JSON.

存储的数据将是一堆记录,每个记录由一组字段组成。因此,存储在可以逐行读取的文本文件中并不够简单。这就是为什么我认为我需要像JSON这样的东西。但我不想只是为了解析格式而添加外部库。有什么建议?我是Java新手。

The data stored will be a bunch of records, each record composed of a set of fields. So it's not simple enough to be stored in a text file that can be read line by line. This is why I think I need something like JSON. But I don't want to add external libraries just to parse the format. Any suggestions? I'm new to Java.

推荐答案

虽然Java很多没有标准 JSON解析库,有几个库可用,快速,可靠,易于使用。许多还允许您使用标准对象绑定方法,如 JAXB 使用注释定义反序列化映射。

While Java many not have a standard JSON parsing library, there are several libraries available that are fast, reliable, and easy to use. Many also allow you to use standard object binding methodologies such as JAXB do define your deserialization mappings using annotations.

我更喜欢杰克逊我自己。 Google-GSon也很受欢迎,您可以在问题中看到有些人如何比较这两者。

I prefer Jackson myself. Google-GSon is also popular, and you can see how some people compare the two in this question.

您可能希望不那么害怕使用外部库。利用具有所需功能的现有库几乎总是更好,而不是编写自己的库。使用 Maven 常春藤从项目定义中自动计算和下载依赖项,没有理由担心使用库。

You might want to be less afraid of using external libraries. It's almost always better to leverage an existing library that has the functionality you want, rather than to write your own. And with tools like Maven or Ivy to automatically calculate and download dependencies from your project definition, there's really no reason to fear using libraries.

这就是说,在Java XML支持的当前状态下,您应该发现XML可以同等访问。 此答案提供了使用 javax.xml.parsers.DocumentBuilder 生成DOM。

That being said, with the current state of Java XML support, you should find XML equally accessible. This answer provides a simple example of using javax.xml.parsers.DocumentBuilder to generate a DOM.

这篇关于Java内置的JSON或XML数据解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆