1

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.

Results of the for-loop showing the field values

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".

Null output

1 Answer 1

1

Can you try to create response wrapper as below

public class response{
public String Name {get; set;}
public String Content_Copy {get; set;}

public response(String Name, String Content_Copy){
    this.Name = Name;
    this.Content_Copy = Content_Copy;
}


public class responseWrapper{

public response fields {get; set;}

}

}

then you can use the standard JSON.deserialize to get a list of the response wrapper (fields)

String jsonListString = '[{"fields":{"Name":"Step 1","Content_Copy":"lorem"}},{"fields":{"Name":"Step 2","Content_Copy":"ipsum"}}]';
List<response.responseWrapper> response = (List<response.responseWrapper>)System.JSON.deserialize(jsonListString, 
List<response.responseWrapper>.class);
system.debug('response ' +response);
for(response.responseWrapper f : response){
System.debug('fileds: ' + f.fields);
System.debug('name: ' + f.fields.name);
System.debug('Content_Copy: ' + f.fields.Content_Copy);
}
Sign up to request clarification or add additional context in comments.

2 Comments

I followed your suggestion but am just getting 'null' for both values. I've edited my original post to show that.
@ThatBrayKid I have update the answer it should be working now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.