Method call with parameters via JSON in Java
I am receiving JSON messages from the API (I have full control over the messages sent). The messages look like this:
{
"function": "function_name",
"arguments": { "arg1": "Value", "arg2": "Value"}
}
I want to use reflection to call the correct method with the correct parameters and in right order . The problem with this code is that the JSONObject argument conversion does not preserve the order of the parameters (which is normal if JSON is by definition unordered). I will need some kind of mapping to the name of the parameters.
Here is my Java code:
String function_name = (String)json.get("function");
ArrayList<Class> params = new ArrayList<Class>();
ArrayList<String> values = new ArrayList<String>();
JSONObject args = (JSONObject)json.get("arguments");
if (args != null) {
Set<String> keysargs = args.keySet();
for (String key : keysargs) {
params.add(args.get(key).getClass());
values.add(args.get(key).toString());
}
}
Method method;
try {
if (params.size() == 0) {
method = this.getApplication().getClass().getMethod(function_name);
}
else {
method = this.getApplication().getClass().getMethod(function_name, params.toArray(new Class[params.size()]));
}
try {
method.invoke(this.getApplication(), values.toArray());
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
source to share
You should use the JSON array Raman suggested to preserve the order of the arguments. As Java doesn't have a named pass parameter. JSON should look like this:
{
"function": {
"name": "function_name",
"args": ["val1", "val2", "val3"]
}
}
You can even call multiple functions in a specific order
{
"functions": [
"function": {
"name": "function_name1",
"args": ["val1", "val2"]
}
... more functions
]
}
The argument must be of the same type and a string is recommended because the array must be the same type in Java. Anyway, if you want JSON to pass information about the arguments, you can do it like this
{
"function": {
"name": "function_name",
"args": [
{ "name": "val1", "type": "int", "value": "1" },
{ "name": "val2", "type": "string", "value": "something" }
]
}
}
All data types in JSON must be strings as you can create a utility to convert the type based on the value type
.
source to share
So, I used the JANON structure of raman and hussachai and then retrieved data like this:
JSONArray args = (JSONArray)json.get("arguments");
ArrayList<JSONObject> listOfParams = new ArrayList<JSONObject>();
for (int i = 0; i < args.size();i++) {
JSONObject argument_name_value = (JSONObject)args.get(i);
Set<String> keysargs = argument_name_value.keySet();
for (String key : keysargs) {
params.add(argument_name_value.get(key).getClass());
values.add(argument_name_value.get(key).toString());
}
}
It worked smoothly. Many thanks
source to share