function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Corey Edwards 11Corey Edwards 11 

Apex Map<String,Object> need to get object values

I have a JSON string:
 
{addressDetails=({addressId=56941, addressLine1=76 Kilaben Rd, addressLine2=null, addressLine3=null, addressType=H, countryCode=AUS, locality=KILABEN BAY, postcode=2283, sortplan=017, state=NSW, ...}), aliasName=null, concessionInfo=null, contactDate=null, contactDetails=({contactDetail=, contactId=122827, contactType=H, delta=2}), contactPreference={correspondanceLevel=0, correspondanceLevelDescription=null, emailOpt=N, smsOpt=N}, deceasedFlag=N, dob=1952-07-29T00:00:00, firstContactDate=null, firstName=John, ...}
I'm mapping this using:
Map<String, Object> meta = (Map<String, Object>) JSON.deserializeUntyped(viewPersonValue);
To get firstname I'd say:
 
meta.get('firstName');
The issue I'm facing is accessing the values in addressDetails.
I've tried addressDetails[0] and addressDetails.addressId but I think I'm approaching this wrong.
Any help would be greatly appreciated.
 
NagendraNagendra (Salesforce Developers) 
Hi Corey,

Please find the suggested workaround from the stack exchange community for the same issue.

if you make use of the Apex type system, you will have an easier time working with the data. If you create a class for the incoming data structure, abbreviated as:
public class Meta{
   public list<addressDetail> addressDetails {get; set;} 

   public class addressDetail{
       public Integer addressId {get; set;}
   }   
}
You will be able to access the data from your JSON strings as so:
Meta MyMeta = (Meta) JSON.deserialize(viewPersonValue,Meta.class);
system.debug(MyMeta.addressDetails[0].addressId);
Hope this helps.

Please mark this as solved if it's resolved so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue.

Thanks,
Nagendra.