0

This question already has an answer here:

I'm new to apex rest services.Normally in Apex rest services the input json body automatically deserializes to apex class object. but my json body contains reserved key words.For Rest services automatically deserialization happen if we define method parameters so we don't have control to replace json string.

Can anyone please let me know how to overcome this problem.

I'll receive input in the following way

{

"reqId":"DNMFJP00029279",

"buyerDetails":{

"company":"DNMF(DunderMifflin)",

"coordinatorLastName":"Hamilton",

"coordinatorFirstName":"Susan",

"department":"PaperProcessing",

},

"rates":{

"taxStatus":"w-2",

"currency":"USD",

"billRateMax":"30.88",

"billRateMin":"8.00",

"payRateMin":"7.50",

"payRateMax":"22.00",

"targetPayRate":"45.00",

"targetBillRate":"72.00"

},

"reqDetails":{

"responseDeadline":"2015-2-10T00:00:00+00:00",

"category":"Professional",

"reason":"Project",

"education":"Bachelor'sDegree(Required)",

"skills":"MicrosoftWord-4years(Required)",

"workDays":"Monday,Tuesday,Wednesday,Thursday,Friday",

"endTime":"5:00PM",

"startTime":"8:00AM",

"createdDate":"2015-1-31T00:00:00+00:00",

"totalOpenings":1,

"hours":"40.00",

"endDate":"2015-12-31T00:00:00+00:00",

"startDate":"2015-04-13T00:00:00+00:00",

"comments":"WillrequireoccasionaltripstoScranton,PA",

"description":"Thisjobrequiresexpertiseinsalesandpapermanagement.",

"qualifications":"Mustbeabletooperateastapler",

"title":"BusinessAnalystIII",

"positionType":"Temp",

"location":"1725SloughAvenue,WashingtonDCUSA20008"

},

"submittalMax":2,

"reqStatus":"hold",

}

please help me how to deserialize the above string in apex rest service

marked as duplicate by Daniel Blackhall, Jenny B, Chris Duncombe, Simon Lawrence, Samuel De Rycke Jun 23 '15 at 11:53

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

You can use JSON class.

If you have a model you can use JSON.deserialized Ex

String myJson = '{name="Test",phone="1234567890"}';
Account ac = (Account) JSON.deserialize(myJson, Account.class);

If you dont want use a model then you can use JSON.deserializeUntyped Ex:

String myJson = '{name="Test",phone="1234567890"}';
Map<String,Object> ac = JSON.deserializeUntyped(myJson);
System.debug(ac.get('name'));
System.debug(ac.get('phone'));

Best Regards

0

The easiest way I've found it to alter the body of the response before deserializing. It's not ideal but I'm not sure there's a better way.

So, take 'date' for example. This is reserved & cannot be used as a class member name.

// We need to change the name of the 'date' member to 'date_x'
String responseBody = httpResponse.getBody().replaceAll('\"date\":', '\"date_x\":');

Then you can deserialize into your object as normal, but using the string responseBody instead.

MyResponseObject myResponse = (MyResponseObject) JSON.deserializeStrict(responseBody, MyResponseObject.class);

Obviously you will need to alter the member name in your response object to use the new date_x name:

public class MyResponseObject {

    public Date date_x;
}

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