4

I have deserialized some JSON into lists of objects:

Map<String, Object> Results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());        

List<Object> ValueLists = (List<Object>)results.get('values');

List<Object> FirstList = (List<Object>) ValuesLists[0]; 

String FirstString = String.valueOf(FirstList[0]);

All of that works fine. "ValueLists" looks like this, which to me looks like a list of lists:

((node-35351t9s_country_financial, system.devices.seen, 81859, 1578010220741491968), (node-35351t9s_country_financial, system.users.unique, 19494, 1578010220741550080))

But when I try to access the indexes of these lists to create a map, I get "Expression must be a list type: Object".

Map<Object, Object> ServerHostToUsers = new Map<Object, Object>();              
for(Object Obj: ValuesLists){
    ServerHostToUsers.put(Obj[0], Obj[1]);}

I can tell there is some sort of conversion from Object to String that I need, but I can't figure out why this won't work to populate the map, when accessing the indexes worked just fine for populating the lists before it.

Appreciate any nod in the right direction!

1 Answer 1

3

At that point in your code, Apex only knows that Obj is an Object, because you told the compiler that Obj is an Object, but not a List. You'll need to do some casting to get this to work:

for(Object Obj: ValuesLists){
    List<Object> parts = (List<Object>)Obj;
    ServerHostToUsers.put(parts[0], parts[1]);
}
1
  • Thanks Fox, I was just a line away and that was it. Just realized that it was working in the previous code because those variables were declared as lists.
    – number41
    Jan 3, 2020 at 6:52

You must log in to answer this question.

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