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
  • 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, 2018 at 7:56

1 Answer 1

1

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
        }
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .