Domanda

I'm trying to parse some JSON data, and I'm using the examples from this site: http://answers.oreilly.com/topic/257-how-to-parse-json-in-java

I'm using the data from section 2 and the code from section 3, and json-lib as my JSON library.

But when I tried to run the sample, the JSONSerializer class about a missing a language class from Apache Commons. I downloaded that, and it complained of a missing logging class. I downloaded that, and it complained of the missing EZmorph class. I downloaded that, and it complained of a missing Collections class. I then downloaded an unofficial JAR file with all the Apache Commons components, and it complained of a missing SLF4J logger or something. At least point, I gave up and decided to try Google's GSON instead.

This seems much more complicated than it should be. Just how many packages do I need do download? Is there a way to disable logging? Or am I doing something wrong?

È stato utile?

Soluzione

I would recommend using another, more commonly used Java JSON library: for example Jackson or GSON. There are lots of tutorials and articles on using both, and I don't think there are many reasons to use json-lib over either one.

Altri suggerimenti

You can also have a look at Genson. It is a all in one library, with nice features, easy to use and with good performances.

For example to serialize a full pojo to json and then deserialize it back :

 Genson genson = new Genson();
 String json = genson.serialize(yourPojo);

 // and deserialize it
 YourPojo pojo = genson.deserialize(json, YourPojo.class);

As Staxman says, there is no reason to use json-lib. I am still surprised of how many people are using it when there a couple of better libraries.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top