1

Below is the sample JSON string I received from our web service:

{
    "id": 1,
    "result": {
        "0": 15,
        "1": 16
    },
    "error": null
}

Please notice that in the result section, the number of returned parameters can be increased. And we no idea how many will the greatest number be before execution. And it is not an array. Is there any way we can parse this JSON string into an Apex object?

No is an acceptable answer but it would require us to update the API.

  • 1
    I don't think it can be converted to an apex object in your case.won't JSON.deserializeUntyped() to iterate result section data as key-value pair(Map) help ? – Praveen Jul 7 '16 at 6:13
4

Create a separate wrapper class

Class ResponseWrap {
    public Integer id;
    public Map<String, Integer> result; //Use proper data types according to your requirement these are samples provided
    public String error;
}

Call this method where you want to handle the response

public ResponseWrap class webserviceResponseHandler(String responseJSON) {
    try {
        ResponseWrap response = (ResponseWrap)JSON.deserialize(responseJSON, ResponseWrap.class);
    } catch(Exception ex) {
        //handle exception
    }

    return response;
}

Method webserviceResponseHandler() will return you the JSON parser result.

2

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}]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.