X

JsonParser and JsonReader - Streaming and Object Readers in Java API for JSON Processing (TOTD #206)

Guest Author


JsonReader reads a JSON object or array from an input
source. This provides DOM access to the JSON structure.



JsonParser provides forward, ready-only access to JSON
data in a streaming way. The parser can be created from InputStream
and Reader. A sample code looks like:



JsonParser jsonParser = Json.createParser(new
StringReader(YOUR_STRING_HERE));




Here is a table with code fragments for parsing some common JSON
using Object Model and events generated from Streaming API:
































JSON Object Model API Events from Streaming API
{ }

JsonReader jsonReader =

    new JsonReader(new StringReader("{}"));

JsonObject json = jsonReader.readObject();

{START_OBJECT }END_OBJECT

{

  "apple":"red",

  "banana":"yellow"

}

jsonReader =

    new JsonReader(new StringReader(...));

json = jsonReader.readObject();


{START_OBJECT

  "apple"KEY_NAME:"red"VALUE_STRING,

  "banana"KEY_NAME:"yellow"VALUE_STRING

}

[

  { "apple":"red" },

  { "banana":"yellow" }

]

jsonReader =

    new JsonReader(new StringReader(...));

JsonArray jsonArr = jsonReader.readArray();


[START_ARRAY

  {START_OBJECT "apple"KEY_NAME:"red"VALUE_STRING
}END_OBJECT,


  {START_OBJECT "banana"KEY_NAME:"yellow"VALUE_STRING
}END_OBJECT


]END_ARRAY

{

  "title":"The Matrix",

  "year":1999,

  "cast":[

    "Keanu Reaves",

    "Laurence Fishburne",

    "Carrie-Anne Moss"

  ]

}

jsonReader =

    new JsonReader(new StringReader(...));

json = jsonReader.readObject();


{START_OBJECT

  "title"KEY_NAME:"The Matrix"VALUE_STRING,

  "year"KEY_NAME:1999VALUE_NUMBER,

  "cast"KEY_NAME:[START_ARRAY

    "Keanu Reaves"VALUE_STRING,

    "Laurence Fishburne"VALUE_STRING,

    "Carrie-Anne Moss"VALUE_STRING

  ]END_ARRAY

}END_OBJECT




The source code for this sample can be href="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/File/5d2af471f452a6133d091baae3f4eb4d/totd206_samples.zip">downloaded
here, try it with href="http://dlc.sun.com.edgesuite.net/glassfish/4.0/promoted/glassfish-4.0-b76.zip">GlassFish
4 b76 or href="http://dlc.sun.com.edgesuite.net/glassfish/4.0/promoted/">later.



Note, GlassFish b76 has href="http://json-processing-spec.java.net/nonav/releases/1.0/pr-draft/javadocs/index.html">Public
Review version of the APIs. But the href="http://json-processing-spec.java.net/nonav/releases/1.0/pfd-draft2/javadocs/index.html">APIs
have evolved since then and an integration is planned in the
next few days. So if you are using an older build, the sample code
may not work. I'll update the sample code once the bits are
integrated.



Here are some more links for you to read further:
  • href="http://json-processing-spec.java.net/nonav/releases/1.0/pr-draft/javadocs/index.html">Public
    Review javadoc
  • href="http://json-processing-spec.java.net/nonav/releases/1.0/pfd-draft2/javadocs/index.html">Latest
    javadoc
  • Reference Implemenation: jsonp.java.net



Join the discussion

Comments ( 1 )
  • Gabor Gossmann Wednesday, February 20, 2013

    Hello Arun,

    If the Json can have own type. When it can be in vector.and we put under it matrix (or more vectors)(a plus work): its much better as XML nad it's not duplicated, not hard to read a person who don't learn it.It's great!


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.