Activity › Forums › Salesforce® Discussions › How to map JSON string with Salesforce object?
Tagged: JSON, Lead, Wrapper Class
-
How to map JSON string with Salesforce object?
Posted by Audrey on April 25, 2016 at 10:54 AMHow to map JSON string with Salesforce object (consider Lead)?
shariq replied 7 years, 9 months ago 4 Members · 5 Replies -
5 Replies
-
You have to create a wrapper class for that, and the variables in the fields must be same as the objects in the JSON, and after that use this syntax.
list<WrapperClass>responseList = (WrapperClass)System.JSON.deserialize(jsonResponse,MailgunParser.class)
If you provide your code then I can provide you a complete solution.
- [adinserter block='9']
-
Hi,
Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(jsonInput);
Map<String, Object> m2=(Map<String, Object>)m.get(‘info5’);
List<decimal> d=new List<decimal>();for(String s:m2.keyset()){
decimal t=(decimal)m2.get(s);
d.add(t);
} -
Approach 1
Type resultType = Type.forName(‘CustomersResponse’);
CustomersResponse deserializeResults = (CustomersResponse)JSON.deserialize(response, resultType);
System.debug(‘==========> deserialize() results = ‘ + deserializeResults);NOTE: With the above code, in theory, it is possible to use JSON.deserialize(response,CustomersResponse.class), however this does not always work. Sometimes Salesforce is unable to determine the type correctly and you receive a “variable does not exist : CustomerDetails.type” compile error message). So instead, you must first use a Type.forName() call to take care of this problem. Alternatively, you could compile all classes in your Salesforce Org together or define your class as an inner class of the class doing the deserialization work.
Approach 2
Type resultType = Type.forName(‘CustomersResponse’);
CustomersResponse readValueAsResults = (CustomersResponse)JSON.createParser(response).readValueAs(resultType);
System.debug(‘==========> createParser().readValueAs() results = ‘ + readValueAsResults); -
Hi,
Try this –
ABC obj = (ABC)JSON.deserialize(responseBody, ABC.class)
Hope this helps.
-
Hi,
You can also use Wrapper class for it.
Hope this helps.
Log In to reply.