RSS

JSON Arrray parsing with com.ibm.commons.util.io.json.JsonJavaFactory

11 Sep

I’m currently developing the backendservices for our new absenceplanner. We submit all data to a rest service (CustomRestService in the ExtLib). So this service should be able to parse an array of json data. The data has the following format:

{
“dates”:[
{“div”:”dt20120827″,”dt”:”27.08.2012″,”metadata”:{“absenceType”:”Holiday”,”allDay”:true}},
{“div”:”dt20120829″,”dt”:”29.08.2012″,”metadata”:{“absenceType”:”Compensation”,”allDay”:true}}
],
“reqTitle”:”meine wohlverdienten ferien”,
“method”:”request.submit”
}

To parse the stream in to a JsonJavaObject I’ve used:

JsonJavaFactory factory = JsonJavaFactory.instanceEx;
Reader r = request.getReader();
json = (JsonJavaObject) JsonParser.fromJson(factory, r);
String strMethod = json.getString(“method”);

It’s very easy to extract all properties. An object can also be accessed with json.getJsonObject(“name”) . But no arrays. There is no getter.

But, how did others (like the ExtLib programmers) solve this? With this question I’ve read the code of the “twitter parsing” in the sbt of the ExtLib. And I found the solution in the com.ibm.xsp.extlib.sbt.services.client.DataNavigator class, which guides me to the following code:

 

Object arrDates = factory.getProperty(json, “dates”);
for (Iterator<Object> itDate = factory.iterateArrayValues(arrDates); itDate.hasNext();) {

JsonJavaObject jsDate = (JsonJavaObject)itDate.next();
//-> here do all the funny stuff with the jsDate object.
}

Thanks to Philippe Riand (IBM) for sharing your code. Its so inspiring to read your code.

 
6 Comments

Posted by on September 11, 2012 in Java, XPages

 

Tags: , , , ,

6 responses to “JSON Arrray parsing with com.ibm.commons.util.io.json.JsonJavaFactory

  1. Jeff Byrd

    March 15, 2013 at 4:29 pm

    Thanks for posing this. Saved me a lot of time!

     
  2. Frank van der Linden

    June 19, 2013 at 2:05 pm

    It saved my day, thanks for this information

     
  3. Oliver Busse

    April 11, 2014 at 9:59 pm

    Argh, why didn’t I find this before struggeling with JacksonJSON?
    Thanks for sharing and thanks to me for finding it 😉

     
    • guedebyte

      April 11, 2014 at 10:02 pm

      🙂 I know that feeling. If you wanna produce JSON Output from obejct, you should also take a look at the XPagesToolkit

       
  4. corejavatraininginhyderabad

    May 2, 2017 at 2:38 pm

    Hi,
    Very nice and informative article. The information provided saved my time and was of great use. Thanks for posting this article. Keep posting.

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: