I am trying to populate this Set (buildingIds) with string values, but I am getting the error: Method does not exist or incorrect signature: void add(Object) from the type Set.
I am returning some json and iterating through it trying to exact the "building ids" into a Apex Set to ensure uniqueness.
If I dump out the values using: listingAttribute.get(attributeName) within my for loop I see the value for the building id at each iteration of the loop, but it errors when I try to add it to my set.
My code is below:
Map<String, Object> jsonResults = (Map<String, Object>) JSON.deserializeUntyped(JSONResponse);
Set<String> buildingIds = new Set<String>();
List<Object> listBuildings = (List<Object>)jsonResults.get('buildings');
for (Object buildingRecord: listBuildings ) {
Map<String, Object> buildingAttributes = (Map<String, Object>)buildingRecord;
for (String attributeName : buildingAttributes .keySet()) {
if( attributeName == 'building_id') {
buildingIds.add(buildingAttributes.get(attributeName)); // **errors here**
}
}
} // close outer for loop
How can I add the building Id to the set?
Best Answer
Each of these two approaches below should work:
buildingIds.add((String)buildingAttributes.get(attributeName));
Or
buildingIds.add(buildingAttributes.get(attributeName).toString());
The problem is you try to add an Object into a Set of string.