1

How do i use JSON to parse out the following result i get back from Rally's web service api.

{"QueryResult": {"_rallyAPIMajor": "2", "_rallyAPIMinor": "0", "Errors": [], "Warnings": ["It is no longer necessary to append \".js\" to WSAPI resources."], "TotalResultCount": 4, "StartIndex": 1, "PageSize": 200, "Results": [{"_rallyAPIMajor": "2", "_rallyAPIMinor": "0", "_ref": "https://rally1.rallydev.com/slm/webservice/v2.0/task/16883055138", "_refObjectUUID": "038c4641-71f7-4a95-a663-3555a3c369dc", "_objectVersion": "5", "_refObjectName": "Validated the changes in SO Request Section", "Name": "Validated the changes in SO Request Section", "Actuals": 1.0, "State": "Completed", "_type": "Task"}, {"_rallyAPIMajor": "2", "_rallyAPIMinor": "0", "_ref": "https://rally1.rallydev.com/slm/webservice/v2.0/task/16947438122", "_refObjectUUID": "c58b3dfb-50a4-414c-94d0-d341181c7d15", "_objectVersion": "5", "_refObjectName": "Change WAD list to show only opportunities", "Name": "Change WAD list to show only opportunities", "Actuals": 1.0, "State": "Completed", "_type": "Task"}, {"_rallyAPIMajor": "2", "_rallyAPIMinor": "0", "_ref": "https://rally1.rallydev.com/slm/webservice/v2.0/task/16947441630", "_refObjectUUID": "727b8a6c-1305-416a-a237-1ebeaddf515e", "_objectVersion": "5", "_refObjectName": "Add new fields to Opportunity", "Name": "Add new fields to Opportunity", "Actuals": 1.0, "State": "Completed", "_type": "Task"}, {"_rallyAPIMajor": "2", "_rallyAPIMinor": "0", "_ref": "https://rally1.rallydev.com/slm/webservice/v2.0/task/16949115293", "_refObjectUUID": "c057615e-7a5a-4f05-a600-56b4c565ad36", "_objectVersion": "2", "_refObjectName": "Validate the changes", "Name": "Validate the changes", "Actuals": null, "State": "Defined", "_type": "Task"}]}}

1 Answer 1

2

You have to deserialize the response body you are getting. To achieve that you need to use the apex JSON lib.

From my view the easiest method is use json.deserializeUntyped

Sample

    Http h = new Http();
    Httpresponse resp = h.send(req);
    system.debug('resp:'+resp);
    system.debug('respBody:'+resp.getBody());
    Map<String,Object> jsonResp = (Map<String,Object>) JSON.deserializeUntyped(resp.getBody());
    Map<String,Object> res = (Map<String,Object>) jsonResp.get('QueryResult');
    List<Object> results = ((List<Object>)) results.get('Results');

    for(Map<String,Object> r: results){
       system.debug('NAme: '+r.get('Name'));
       system.debug('State: '+r.get('State')); 
        //rest ..... 
    }
2
  • Thanks, made a few changes : Map<String,Object> jsonResp = (Map<String,Object>) JSON.deserializeUntyped(res.getBody()); Map<String,Object> qres = (Map<String,Object>) jsonResp.get('QueryResult'); List<Object> resultsObject = (List<Object>)qres.get('Results'); for(Object r: resultsObject ){ Map<String,Object> testResult=( Map<String,Object>) r; system.debug('NAme: '+testResult.get('Name')); system.debug('State: '+testResult.get('State')); }
    – batrulz
    Commented Feb 14, 2014 at 17:39
  • But i had a question , the 'Results' will be different based on what i add in the query to the service, so do i have to modify the parsing every time ?
    – batrulz
    Commented Feb 14, 2014 at 17:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .