I want to run a class where it calls the static resource and input the values in the object.
I have a Static Resource name Interest_Tag MIME Type application/json in following JSON format
[
{
"NAME": "Healthcare",
"COGNITIVE_UNIQUE_ID": 10622212216
},
{
"NAME": "Real Estate",
"UNIQUE_ID": "106ff12216"
},
{
"NAME": "Investigo",
"UNIQUE_ID": 12516177889
},
{
"NAME": "Verna",
"UNIQUE_ID": "agagagha1215"
}
]
My Class
public class InterestTags_Insert {
public static void InterestTags_Insert1(){
List <DevNiks__Interest_Tag__c> IT = new List<DevNiks__Interest_Tag__c>();
StaticResource sr= [select id,body from StaticResource Where Name = 'Interest_Tag'];
String allcontents = sr.body.toString();
system.debug('allcontents==='+allcontents);
String str = JSON.serialize(allcontents);
system.debug('New String = '+str);
List<StaticResource_InterestTags> StaticResource_InterestTagsResults = (List<StaticResource_InterestTags>)JSON.deserialize(str, List<StaticResource_InterestTags>.class);
System.debug('StaticResource_InterestTags' +StaticResource_InterestTagsResults);
for(StaticResource_InterestTags srt :StaticResource_InterestTagsResults ){
DevNiks__Interest_Tag__c ditc = new DevNiks__Interest_Tag__c();
ditc.name = srt.NAMEE;
if(srt.UNIQUE_ID!=null){
ditc.DevNiks__Unique_ID__c = String.valueof(srt.UNIQUE_ID);
}
IT.add(ditc);
System.debug('IT List ==='+IT);
}
Database.insert(IT);
}
}
My Parser Class
public class StaticResource_InterestTags {
public String NAMEE;
public Integer UNIQUE_ID;
public static List<StaticResource_InterestTags> parse(String json) {
return (List<StaticResource_InterestTags>) System.JSON.deserialize(json, List<StaticResource_InterestTags>.class);
}
}
Best Answer
The variable
allcontents
is already a JSON string so you don't need to serialize it.So replace:
with:
(When you serialize it, you turn the JSON representing an array into JSON representing single string value, hence the error message.)