Skip to main content

I have a API REST that return a JSON:

I have a dude about to solve a Error Conversion from runtime type map<string,any> to list<any>

My code is:

 

apex.png

and my Visualforce is (Javascript):

 

vf.png

 

How to get all data and print inside of the table en HTML(Visualforce page) ???

 

When a try to get all data i get a ERROR:

FATAL_ERROR System.TypeException: Invalid conversion from runtime type Map<String,ANY> to List<ANY>

 

I try to use information of :

 

  1. https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_Json.htm
  2. https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_Json.htm

but i don't have a good solution.

 

Can you explain me how to solve this error or problem ?

#Collaboration #Apex #Visualforce Pages #Data Management #Ask An Expert #Lightning

4 Antworten
  1. 5. Juli 2021, 23:00

    You can not deserialize your JSON object into a list<anything> because your JSON is not representing a list. If I don't mess things up a list is in the JSON notation marked with [ listContent ]. Your debug message shows curly brackets { which are used for an object.  If you only get one instance of an object, do it that way: 

    Wrappers wrappers = (Wrappers) JSON.deserialize(res.getBody(), Wrappers.class);

     

    And of course for the other way:

    Start your JSON string with [ and end it with ]. Then parse it with:

    List<Wrappers> wrappersList = (List<Wrappers>) JSON.deserialize(res.getBody(), List<Wrappers>.class);

     

    Respect case sensitivity if you define an Apex class for JSON deserialization. (But I think you did that part ;))

     

    The JSON Apex class is well documented: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_Json.htm

     

    And another crazy link, this time to w3schools about JSON notation :D https://www.w3schools.com/js/js_json_arrays.asp

     

     (And a little side thought: Write Apex classes in PascalCase :D if there is no other naming convention in your company.)  If you have any questions feel free to ask. Happy Coding ;)

0/9000