function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Gaurav Gulanjkar 18Gaurav Gulanjkar 18 

Deserializing a JASON in apex class

Hi all,

I am trying to deserialize a jason string in a class, like this :
@RemoteAction
    public static submit(String objString)
{
Map<String,Object>  dataListMap = (Map<String,Object>)JSON.deserializeUntyped(objString);
        List<Object> dataList = (List<Object>)dataListMap.get('question');
        
       	for(Object objSobject : dataList){
            Map<String, Object> p = (Map<String, Object>) objSobject; 
          }
}

Following is the JASON string :
"ansList": [ { "CP_SurveyQuestion__c": "a228E000000CcvAQAS", "Id": "a238E000000CowKQAS", "Name": "Highly Satisfied" }, { "CP_SurveyQuestion__c": "a228E000000CcvAQAS", "Id": "a238E000000CowZQAS", "Name": "Does not work" }, { "CP_SurveyQuestion__c": "a228E000000CcvAQAS", "Id": "a238E000000CowUQAS", "Name": "Not Satisfied" }, { "CP_SurveyQuestion__c": "a228E000000CcvAQAS", "Id": "a238E000000CowPQAS", "Name": "Satisfied" }, { "CP_SurveyQuestion__c": "a228E000000CcvAQAS", "Id": "a238E000000CowFQAS", "Name": "Very Impressed" } ], "question": { "Id": "a228E000000CcvAQAS", "Name": "SQ-000007", "CP_Question__c": "The products offered in the stored", "CP_AnswerType__c": "5 Radio Buttons", "Survey__c": "a208E000000CfOzQAK", "Survey_Answers__r": [ { "CP_SurveyQuestion__c": "a228E000000CcvAQAS", "Id": "a238E000000CowKQAS", "Name": "Highly Satisfied" }, { "CP_SurveyQuestion__c": "a228E000000CcvAQAS", "Id": "a238E000000CowZQAS", "Name": "Does not work" }, { "CP_SurveyQuestion__c": "a228E000000CcvAQAS", "Id": "a238E000000CowUQAS", "Name": "Not Satisfied" }, { "CP_SurveyQuestion__c": "a228E000000CcvAQAS", "Id": "a238E000000CowPQAS", "Name": "Satisfied" }, { "CP_SurveyQuestion__c": "a228E000000CcvAQAS", "Id": "a238E000000CowFQAS", "Name": "Very Impressed" } ] }, "strSelectedAnswer": "Very Impressed"

I get this error in the debug :
System.TypeException: Invalid conversion from runtime type List<ANY> to Map<String,ANY>

I am using JSON.deserializeUntyped and from the VF I am passing angular JS list to the remote function like :angular.toJson($scope.relatedQuesAnsList, true)

Can any one let me know the what i am doing wrong.
Reference 
ShivaKrishna(Freelancer)ShivaKrishna(Freelancer)
Hi Gaurav,

try like this

List<Object>  dataList = (List<Object>)JSON.deserializeUntyped(objString);

you can not deserialise as such as objString is array of records and not the map.

you can create a map from dataList as you require later but first you need to deserialise then process

let me know, if it helps you or need any help :)
shiva.sfdc.backup@gmail.com