0

First i will have a JSON String:

{"API_Name__c":["values 1","value2"],"API_Name2__c":["value3","value4"]}

Here is my apex controller:

public static List<Object> getObject(String filter) {
System.debug('Here is String of map: ' + filter);
        Map<String, List<String>> filterMap = ( Map<String, List<String>>)JSON.deserializeUntyped(filter);
        System.debug('Here is map: ' + filterMap);
}

It will give me this Error:

Invalid conversion from runtime type Map<String,ANY> to Map<String,List<String>>

How should i fix this? Thanks for helping!

  • 1
    you can also use deserialize instead of deserializeUntyped Map<String, List<String>> filterMap = ( Map<String, List<String>>)JSON.deserialize(filter, Map<String,List<String>>.class); – Safiya PV Aug 28 '18 at 7:56
0

According to the documentation, JSON.deserializeUntyped will return an collections of primitive data types, which is primitive Object, that may be List<Object> or Map<String, Object>. So, unfortunately, you can not perform desired operation as is, but you can do the following steps:

  1. Get Map as a first step of deserializing
  2. Get List as second step

Code

public static List<Object> getObject(String filter) {
    System.debug('Here is String of map: ' + filter);
    Map<String, Object> parent_map = ( Map<String, Object>)JSON.deserializeUntyped(filter);
    System.debug('Here is Parent map: ' + parent_map);
    for(String key : parent_map.keySet()) {
        if (parent_map.get(key) instanceof List<Object>) {
            List<Object> values = (List<Object>)parent_map.get(key);
            System.debug('Here is List of key "'+key+'" :' + values);
        } else {
            // handle a case if that is primitive, or other Map
        }
    }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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