You need to sign in to do that
Don't have an account?
Gaurav Gulanjkar 18
Deserializing a JASON in apex class
Hi all,
I am trying to deserialize a jason string in a class, like this :
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
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
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