I'm passing a JSON string from Javascript to Apex. The fields I want to access are "Name" and "Content_Copy__c" for each instance of "fields", but I can't seem to figure it out. Part of me thinks I need to create a map of the map, but I'm also unsure how to do that. In the try-catch, the try does not work, and I think that is part of the problem as it goes into the catch.
String jsonListString = '[{"fields":{"Name":"Step 1","Content_Copy__c":"lorem"}},{"fields":{"Name":"Step 2","Content_Copy__c":"ipsum"}}]';
Object o = JSON.deserializeUntyped(jsonListString);
try{
Map<String, Object> m = (Map<String, Object>) o;
System.debug('The Map is: ' + m);
}
catch(TypeException e){
List<Object> a = (List<Object>) o;
for(Object obj : a){
Map<String, Object> mapObject = (Map<String, Object>) obj;
Object s = mapObject.get('fields');
System.debug(s);
}
}
The above code gives the following results when printed to the debugger.
EDIT: with @Moustafa Ishak 's suggestion, I've changed things around to be like so:
public class fields{
public String Name {get; set;}
public String Content_Copy {get; set;}
public fields(String Name, String Content_Copy){
this.Name = Name;
this.Content_Copy = Content_Copy;
}
}
String jsonListString = '[{"fields":{"Name":"Step 1","Content_Copy":"lorem"}},{"fields":{"Name":"Step 2","Content_Copy":"ipsum"}}]';
List<fields> response = (List<fields>)System.JSON.deserialize(jsonListString, List<fields>.class);
for(fields f : response){
System.debug('Name: ' + f.Name);
System.debug('Content_Copy: ' + f.Content_Copy);
}
Which gives me null values as shown below. I had to change "Content_Copy__c" to "Content_Copy" since Apex has a problem with non-custom object ending in "__c". That won't be an issue once I get the values of the fields. The code above recognized if I add or take away another "fields" entry, but won't recognize the values of "Name" and "Content_Copy".

