-3

I have a Json getting it in Aura Controller, and for test class, I am mimicking the Json into a string .

Original

JSON = [{"subid":"a4wgwe00000DfDRAA0","totalgs":"25","Revenue":"17"},{"subid":"a4wg0000weDfDUAA0","totalgs":"73080","Revenue":"109797"}]

I wanted to pass the String into list<object>** How to do that?

String a='[{"subid":"a4wgwe00000DfDRAA0","totalgs":"25","Revenue":"17"},{"subid":"a4wg0000weDfDUAA0","totalgs":"73080","Revenue":"109797"}]';

my method is submithistory(list<object>)

Tried things

String a='[{"subid":"a4wgwe00000DfDRAA0","totalgs":"25","Revenue":"17"},{"subid":"a4wg0000weDfDUAA0","totalgs":"73080","Revenue":"109797"}]';
system.debug((List<object>) System.JSON.deserialize(a));

and

Map<string,object> obj = (Map<string,object>)json.deserializeUntyped('[{"subid":"a4wgwe00000DfDRAA0","totalgs":"25","Revenue":"17"},{"subid":"a4wg0000weDfDUAA0","totalgs":"73080","Revenue":"109797"}]');

list<object> ob = (list<object>)obj .get('Test');
for(object ooo:ob ){
Map<string,object> o = (Map<string,Object>)ooo ;
}
  • 1
    what have you tried so far? are you stuck somewhere in your code? please edit your post to include this information. thanks – glls Aug 14 '19 at 10:50
  • I have edited the post – Arvind Aug 14 '19 at 10:55
  • and what is the problem? any errors? – glls Aug 14 '19 at 10:55
  • its throwing error .. string to list<object> error so do you think those snippets are correct?? or do you have solutions. – Arvind Aug 14 '19 at 10:56
3

That is List<Map<String,Object>>. However, you cannot do type casting directly. You can do it as below:

List<object> objList = (List<object>)json.deserializeUntyped('[{"subid":"a4wgwe00000DfDRAA0","totalgs":"25","Revenue":"17"},{"subid":"a4wg0000weDfDUAA0","totalgs":"73080","Revenue":"109797"}]');

for(Object obj : objList){
    Map<String,Object> objRec = (Map<String,Object>)obj;
    String subid = (String)objRec.get('subid');
    System.debug(subid);
}
|improve this answer|||||
  • I have to send it in system.debug(PRDApplicationController.submitForApproval(pr.id, objRec , 1.1, 1.1, false)); Getting Error Sas Method does not exist or incorrect signature: void submitForApproval(Id, Map<String,Object>, Decimal, Decimal, Boolean) from the type PRDApplicationController – Arvind Aug 14 '19 at 11:54
0

When you use System.JSON.deserialize you have to specify the type it should deserialize into.

When you specify List<Object>.class in the second param, it parses and returns you what you want.

String a='[{"subid":"a4wgwe00000DfDRAA0","totalgs":"25","Revenue":"17"},{"subid":"a4wg0000weDfDUAA0","totalgs":"73080","Revenue":"109797"}]';
system.debug((List<object>) System.JSON.deserialize(a , List<Object>.class));

Src: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_Json.htm#apex_System_Json_deserialize

|improve this answer|||||

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