You need to sign in to do that
Don't have an account?
![Diwakar G 7 Diwakar G 7](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
Reading values of JSON in Apex
Hi,
How to read the values of below json using List and Maps in Apex.
Thanks and Regards,
Diwakar G
How to read the values of below json using List and Maps in Apex.
String recordstring = '[[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}],[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}],[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}]]'Please help me.
Thanks and Regards,
Diwakar G
Try the below updated one:
String recordstring = '[[[{"usname":"US-002"},{"to":"Diw"}]],[[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}],[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}],[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}]]]';
List<Object> results = (List<Object>)JSON.deserializeUntyped(recordstring);
Map<String,Object> keymap = new Map<String,Object>();
List<Object> objList2 = new List<Object>();
for(Object obj:results)
{
List<Object> objList = (List<Object>)obj;
for(Object obj1 : objList)
{
objList2.add(obj1);
}
}
for(Object obj2:objList2 )
{
List<Object> objList1 = (List<Object>)obj2;
for(Object obj3:objList1)
{
Map<String,Object> keyStr = (Map<String,Object>)obj3;
if(keyStr.containsKey('usname'))
system.debug('@@@ usname ==> ' + keyStr.get('usname'));
else if(keyStr.containsKey('to'))
system.debug('@@@ to ==> ' + keyStr.get('to'));
else if(keyStr.containsKey('File__c'))
system.debug('@@@ File__c ==> ' + keyStr.get('File__c'));
else if(keyStr.containsKey('Rule__c'))
system.debug('@@@ Rule__c ==> ' + keyStr.get('Rule__c'));
else if(keyStr.containsKey('Message__c'))
system.debug('@@@ Message__c ==> ' + keyStr.get('Message__c'));
}
}
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Maharajan.C
All Answers
Please mark it a best if able to resolve your issue.
Thanks
If you want to use the List and Map to Deserialize then refer the below code :
String recordstring = '[[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}],[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}],[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}]]';
List<Object> results = (List<Object>)JSON.deserializeUntyped(recordstring);
List<Map<String,Object>> keymap = new List<Map<String,Object>>();
for(Object obj:results)
{
List<Object> objList = (List<Object>)obj;
for(Object obj1 : objList)
{
Map<String,Object> keyStr = (Map<String,Object>)obj1;
if(keyStr.containsKey('File__c'))
system.debug('@@@ File__c ==> ' + keyStr.get('File__c'));
else if(keyStr.containsKey('Rule__c'))
system.debug('@@@ Rule__c ==> ' + keyStr.get('Rule__c'));
else if(keyStr.containsKey('Message__c'))
system.debug('@@@ Message__c ==> ' + keyStr.get('Message__c'));
}
}
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Maharajan.C
Hi Maharajan,
For the above json it worked. Need your help for below json. It is an array of arraylist. Don't know how to proceed.
Thanks and Regards,
Diwakar G
Try the below updated one:
String recordstring = '[[[{"usname":"US-002"},{"to":"Diw"}]],[[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}],[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}],[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}]]]';
List<Object> results = (List<Object>)JSON.deserializeUntyped(recordstring);
Map<String,Object> keymap = new Map<String,Object>();
List<Object> objList2 = new List<Object>();
for(Object obj:results)
{
List<Object> objList = (List<Object>)obj;
for(Object obj1 : objList)
{
objList2.add(obj1);
}
}
for(Object obj2:objList2 )
{
List<Object> objList1 = (List<Object>)obj2;
for(Object obj3:objList1)
{
Map<String,Object> keyStr = (Map<String,Object>)obj3;
if(keyStr.containsKey('usname'))
system.debug('@@@ usname ==> ' + keyStr.get('usname'));
else if(keyStr.containsKey('to'))
system.debug('@@@ to ==> ' + keyStr.get('to'));
else if(keyStr.containsKey('File__c'))
system.debug('@@@ File__c ==> ' + keyStr.get('File__c'));
else if(keyStr.containsKey('Rule__c'))
system.debug('@@@ Rule__c ==> ' + keyStr.get('Rule__c'));
else if(keyStr.containsKey('Message__c'))
system.debug('@@@ Message__c ==> ' + keyStr.get('Message__c'));
}
}
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Maharajan.C
I thought you have a relation between File, Rule and Message, so I went through a specific class route. If that is not the case, I think then Maharjan code looks fine you can go with that.