Here is how you can solve this with help of map.
Note this constraints:
1) You should be aware about the other schema of JSON
2) There can be n number of elements in result in the form of key val(Sting => Int)
3) it does not handle the nested maps. you will need use recursive functions for that
4) For production implementation i would list all possible form of responses and add validation around it.
Class Resp{
public Integer id;
public String error;
public map <String, Integer> res = new map<String, Integer>();
public Resp (String s) {
JSONParser parser = JSON.createParser(s);
String key;
Integer val;
map<String, integer> result = new map<String,Integer>();
while (parser.nextToken() != null) {
String fieldName = parser.getText();
if(fieldName == 'id'){
parser.nextToken();
this.id = parser.getIntegerValue();
}
if(fieldName == 'error'){
parser.nextToken();
this.error = parser.getText();
}
if(fieldName == 'result'){
while (parser.getCurrentToken() != JSONToken.END_OBJECT && parser.nextToken() != null) {
if( parser.getCurrentToken() == JSONToken.FIELD_NAME) {
key = parser.getText();
}
if( parser.getCurrentToken() == JSONToken.VALUE_NUMBER_INT) {
val = parser.getIntegerValue();
result.put(key, val);
}
if( parser.getCurrentToken() == JSONToken.END_OBJECT) {
this.res = result;
}
}
}
}
}
}
String s1 = '{ "id": 1, "result": { "0": 15, "1": 16 }, "error": null}';
Resp r1 = new Resp(s1);
System.debug(' obj1 ' +r1);
// out put ===> DEBUG| obj1 Resp:[error=null, id=1, res={0=15, 1=16}]
String s2 = '{ "id": 1, "result": { "0": 15, "1": 16, "100": 200 }, "error": null}';
Resp r2 = new Resp(s2);
System.debug(' obj 2 ' +r2);
// out put ===> DEBUG| obj 2 Resp:[error=null, id=1, res={0=15, 1=16, 100=200}]