views:

38

answers:

2

Hello,

we are upgrading our web app to use Facebook's Graph API, which returns JSON responses. However we don't want to add dependecy to a JSON library unless we have no other choice. For server-side http requests we use Apache HttpComponents.

Thus, my question is what are the classes (if any) in the JDK and/or in HttpComponents that I can use to process JSON responses? Code snippets are welcome :)

+2  A: 

There are none is the javax.script API as mentioned by @McDowell, but the JSON syntax is pretty trivial. You could write a stackbased parser yourself which determines the JSON string char-by-char and converts JSON arrays [] to a Object[] or List<Object> and JSON objects {} to a Map<String, Object>. It's a nice learning exercise. However, when you don't want to worry about long term robustness and maintenance, then I'd suggest to just pick one of the Java libraries listed at the bottom of this page. My personal favourite is Google Gson. It can nicely convert complex JSON structures into fullworthy and reuseable Javabeans.

BalusC
+1 for using Gson.
ColinD
For parsing JSON, the spec is here: http://www.ietf.org/rfc/rfc4627.txt
McDowell
+1  A: 

It is possible. Because JSON is valid JavaScript syntax, you can use the built-in JavaScript interpreter via the scripting API to create and object graph, walk that (using the visitor pattern to push data into a Java object, for example).

However, you need to trust the data or you leave yourself open to code injection attacks. To me, this would not be an adequate substitute for a proper JSON parser.

McDowell
Oh, I totally forgot the Java 1.6's `javax.script` API. I however fully agree that this leaves injection holes open. I wouldn't use it. Regardless +1 for enlightenment.
BalusC