Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Built-in data parser for JSON or XML or else [closed]

Tags:

java

json

xml

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.

like image 908
sameold Avatar asked Feb 13 '12 07:02

sameold


People also ask

Does Java have built in JSON parser?

The Java API for JSON Processing (JSR 353) provides portable APIs to parse, generate, transform, and query JSON using object model and streaming APIs. The object model API creates a random-access, tree-like structure that represents the JSON data in memory. The tree can then be navigated and queried.

Does Java natively support JSON?

JSON is a lightweight text-based format that allows us to represent objects and transfer them across the web or store in the database. There is no native support for JSON manipulation in Java, however, there are multiple modules that provide this functionality.

Is JSON parsing faster than XML?

JSON is faster because it is designed specifically for data interchange. JSON encoding is terse, which requires less bytes for transit. JSON parsers are less complex, which requires less processing time and memory overhead. XML is slower, because it is designed for a lot more than just data interchange.

Can an XML parser parse JSON?

JSON is Unlike XML BecauseXML has to be parsed with an XML parser. JSON can be parsed by a standard JavaScript function.


1 Answers

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.

like image 111
ironchefpython Avatar answered Nov 15 '22 23:11

ironchefpython