sameold sameold - 1 year ago 93
Java Question

Java Built-in data parser for JSON or XML or else

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.

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.

Answer Source

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.

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

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.

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.