Question: I have to design a class based on a json template that i received. I'm stuck at this part of the json:

```
events: {
    "door": [
        5,
        {
            valueone: 27,
            valuetwo: "something"
        }
    ],
    "window": [
        2,
        {
            valueone: 13,
            valuetwo: "something"
        }
    ]
}

```

The best i can come up with is this property in the root object:

```
public Dictionary<string, EventData> Events { get; set; }

```

Where Events is defined like this:

```
public class EventData
{
    public int valueone { get; set; }
    public string valuetwo { get; set; }
}

```

This gives me for example the following output:

```
Events: {
    door: {
        valueone: 27,
        valuetwo: "something"
    }
}

```

But i have no idea how to design the class to get the numbers in the json example (5 and 2) in the output. I have tried to google for this for a long time today but i'm not quite sure what to search for.

Answer: 
You can access a method using [`Class.getMethod`](http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getMethod%28java.lang.String,%20java.lang.Class...%29), which returns a [`Method`](http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Method.html) object. You then invoke that `Method` object by passing it the instance and arguments:

```
method.invoke(result.get(Name), whateverArguments);

```

Your question was a bit ambiguous as to whether you're really accessing a *field*. If that's the case, you can get a [`Field`](http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Field.html) via [`Class.getField`](http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getField%28java.lang.String%29), and get its value via the `get` method, again passing in the instance on which you want to get the field.

In either case, the result is typed as `Object`, and you need to downcast that to the expected type. In the case of a primitive like `int`, the object returned will be a boxed type like `Integer`, which you can auto-unbox to `int`:

```
int foo = (Integer) method.invoke(...);

```

The whole thing would look something like:

```
Field field = I2CRegisterValues.getField(fieldName);
Object o = field.get(result.get(Name));
int fieldValue = (Integer) o;

```

... where `fieldName` is a `String` variable in a loop of your `String[] names`. You can also do the above as a one-liner:

```
int fieldValue = (Integer) I2CRegisterValues
    .getField(fieldName)
    .get(result.get(Name));

```
```
     //load desired Class at runtime
  Class classObj = Class.forName("xyz.abc.YourClassName");
  Object obj = classObj .newInstance();

 //invoke specific method at runtime
     for( String methodName : result.keySet() ){
       Method method = cls.getDeclaredMethod(methodName , noparams);
       method.invoke(obj, null);
      }

```