在Java

时间:2015-06-30 18:54:41

标签: java json reflection

我从API接收JSON消息(我完全控制发送的消息)。 消息如下所示:

{
 "function": "function_name",
 "arguments": { "arg1": "Value", "arg2": "Value"}
}

我想使用反射来使用正确的参数并以右侧顺序调用正确的方法。 该代码的问题在于参数的JSONObject转换不保持参数的顺序(根据定义,JSON是正常的,这是正常的)。 我需要的是使用参数名称进行某种映射。

这是我的Java代码:

    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();
    }

3 个答案:

答案 0 :(得分:3)

为什么你没有json数组的参数。您也可以保留订单。 像 -

这样的东西
{
 "function": "function_name",
 "arguments": [{ "arg1": "Value"}, {"arg2": "Value"}]
}

答案 1 :(得分:1)

You should use JSON array as Raman suggested to preserve order of arguments. Since Java doesn't have named parameter passing. The JSON should look like this

{
  "function": {
    "name": "function_name",
    "args": ["val1", "val2", "val3"]
  }
}

You can even call more than one function in specific order

{
   "functions": [
      "function": {
         "name": "function_name1",
         "args": ["val1", "val2"]
      }
      ... more functions
   ]
}

The argument should be the same type and string is recommended because array should be the same type in Java. Anyway, if you want JSON to carry argument information , you can do so as the following

{
  "function": {
    "name": "function_name",
    "args": [
      { "name": "val1", "type": "int", "value": "1" },
      { "name": "val2", "type": "string", "value": "something" }
    ]
  }
}

All data types in JSON should be string as you can create the utility to convert type based on type's value.

答案 2 :(得分:0)

所以我使用了raman和hussachai JSON结构,然后像这样提取数据:

    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());
        }
    }

这很顺利。非常感谢