Android WebView contains a method called addJavascriptInterface(Object obj, String interfaceName) , which should be useful here.
Using this method, the obj object that you add as an interface can be obtained through JavaScript code in a web view. In your case, you can pass an object that has a setter method that brings some JavaScript object back to Java.
You still need to create glue code that converts your JavaScript object to a JSON object. For a quick approach, you can simply create your own interface to create a JSONObject on the Java side using the JSON string passed from JavaScript. The JSONObject class in Java has a constructor that accepts a string containing JSON data. That way, you can pass the string result directly back to Java and create the object this way. For example:
class JSInterface { HashMap<String, JSONObject> mObjectsFromJS = new HashMap<String, JSONObject>(); public void passObject(String name, String json) { mObjectsFromJS.put(name, new JSONObject(json)); } }
Then, from the JavaScript side, in a handler that has blob unparsed JSON in the jsonData variable:
Android.passObject("pageItems", jsonData);
Your Java-sided JSInterface will now have a JSONObject containing elements that you can access using the recipients provided by JSONObject. Objects created using a Javascript call will be displayed on the mObjectsFromJS map. Of course, you'll want to add additional helper methods to the JSInterface class to better manage objects.
I have not compiled or tested any of these methods, so you may need to tweak them a bit to work properly. But hopefully this gives you this idea.
However, if the objects have a consistent interface and data elements, it would be more reasonable to just create a simple JavaScript glue function that connects the properties of a JavaScript object with the fields of objects on the Java side using installation methods.
PLEASE NOTE This enables remote code to run native code on your device. If you do not have full control over the pages / scripts loaded into the WebView , you should make sure that the behavior displayed by obj does not allow any exploits.