Sending and Parsing JSON Objects in Android

Primal Pappachan picture Primal Pappachan · May 12, 2010 · Viewed 160.1k times · Source

I would like to send messages in the form of JSON objects to a server and parse the JSON response from the server.

Example of JSON object

{
  "post": {
    "username": "John Doe",
    "message": "test message",
    "image": "image url",
    "time":  "current time"
  }
}

I am trying to parse the JSON manually by going attribute by attribute. Is there any library/utility I can use to make this process easier?

Answer

StaxMan picture StaxMan · May 15, 2010

I am surprised these have not been mentioned: but instead of using bare-bones rather manual process with json.org's little package, GSon and Jackson are much more convenient to use. So:

So you can actually bind to your own POJOs, not some half-assed tree nodes or Lists and Maps. (and at least Jackson allows binding to such things too (perhaps GSON as well, not sure), JsonNode, Map, List, if you really want these instead of 'real' objects)

EDIT 19-MAR-2014:

Another new contender is Jackson jr library: it uses same fast Streaming parser/generator as Jackson (jackson-core), but data-binding part is tiny (50kB). Functionality is more limited (no annotations, just regular Java Beans), but performance-wise should be fast, and initialization (first-call) overhead very low as well. So it just might be good choice, especially for smaller apps.