tags:

views:

176

answers:

2

I'm writing a java application which runs on the local machine, it needs to interact with another program (also running on the local machine), the other program has a JSON RPC API which is the best way to interact with it. However, in googling around I found a lot of libraries forexposing JSON RPC methods from a java application but nothing good on how to call a remote JSON method. How would I do this?

The two things I'm looking for are:

  1. A way to create new JSON objects to send
  2. A way to connect to the other program and send my response
  3. A way to decode the JSON response
A: 

JSON library usefule to make JSON objects: http://www.json.org/java/ , Lib: http://json-lib.sourceforge.net/

First thing is, JSON call will be AJAX call to some URL. The URL must return JSON content with content type set to "application/json".

You can make a new servlet, in response set above content type, Make new JSONObject, put your objects in it and write the JSONObject.toString() to response.

So the client will get the JSON String. You can access it using simple Javascript OR jQuery.

You can make a special servlet only for JSON support which only haqndles JSON AJAX requests and returns proper JSON responses.

parth.

Paarth
This appears to be describing how to build a web service that receives requests and returns responses in JSON. The questioner appears to be interested in how to consume a JSON RPC service from a plain Java application.
Alan Krueger
anyways... u also specified the same thing i specified... JSON.org :)
Paarth
+1  A: 

For interacting with JSON text in Java, see JSON in Java. Use this for building JSON request objects and serializing them to text as well as de-serializaing the response from the RPC server.

Is this over HTTP or a plain TCP connection? If the former, use HTTPClient. If the latter, just open a socket and use its I/O streams.

Alan Krueger