Activity Forums Salesforce® Discussions How to map JSON string with Salesforce object?

  • Ajit

    Member
    April 25, 2016 at 1:01 pm

    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.

  • Parul

    Member
    September 20, 2018 at 1:15 am

    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);
    }

  • Parul

    Member
    September 20, 2018 at 1:16 am

    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);

  • shariq

    Member
    September 20, 2018 at 7:20 pm

    Hi,

    Try this -

    ABC obj = (ABC)JSON.deserialize(responseBody, ABC.class)

    Hope this helps.

  • shariq

    Member
    September 20, 2018 at 7:21 pm

    Hi,

    You can also use Wrapper class for it.

    Hope this helps.

Log In to reply.

Popular Salesforce Blogs