You need to sign in to do that
Don't have an account?
sharan kumar Kolluru
How to Deserialize JSON Response and Store the Response in Custom Object.
Hi Friends,
I Have a Requirement, I Need to Deserialize the JSON Response and Store in to custom Object.
My JSON Response Format:
{
"_links": {
"_self": "/proxy",
"_parent": "/"
},
"ip": "92.252.241.11",
"port": 4145,
"protocol": "socks4",
"anonymity": "high anonymity",
"lastTested": "2020-06-19 09:55:54",
"allowsRefererHeader": true,
"allowsUserAgentHeader": true,
"allowsCustomHeaders": true,
"allowsCookies": true,
"allowsPost": true,
"allowsHttps": true,
"country": "RU",
"connectTime": "3.312",
"downloadSpeed": "50.000",
"secondsToFirstByte": "3.472",
"uptime": "64.125"
}
To Achieve this Requirement, I Have created a custom Object as Proxy_Server__c and Some Custom Fields to store the Response.
But I'm getting an Error as Compile Error: Incompatible types since an instance of Map<String,Object> is never an instance of List<Object> at line 19 column 34
public class GetProxyDataCallout {
public static HttpResponse makeGetProxyDataCallout(){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api.getproxylist.com/proxy');
request.setMethod('GET');
HttpResponse response = http.send(request);
System.debug(response);
System.debug(response.getBody());
List<Proxy_Server__c> upsertGetProxyList = new List<Proxy_Server__c>();
//Deserialize The JSON Response in to List of Objects
Map<String, Object> results = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());
System.debug(results);
List<Object> proxyList = (List<Object>)results;
System.debug(proxyList);
for(Object proxyListobj : proxyList){
System.debug(proxyListobj);
Map<string, Object> proxyAddress = (Map<string, Object>)proxyListObj;
System.debug(proxyAddress.get('ip'));
Proxy_Server__c proxyIpList = new Proxy_Server__c();
proxyIpList.Name = (String)proxyAddress.get('country');
proxyIpList.IP_Address__c = (String)proxyAddress.get('ip');
proxyIpList.Port__c = (String)proxyAddress.get('port');
proxyIpList.Last_Update__c = (String)proxyAddress.get('lastTested');
proxyIpList.Proxy_Level__c = (String)proxyAddress.get('anonymity');
proxyIpList.Provider__c = 'Get Proxy';
proxyIpList.Proxy_Provider__c = string.isNotBlank(proxyServerId)?proxyServerId:'';
upsertGetProxyList.add(proxyIpList);
}
UPSERT upsertGetProxyList;
return response;
}
}
Please Help me to resolve the issue.
Thanks In Advance.
I Have a Requirement, I Need to Deserialize the JSON Response and Store in to custom Object.
My JSON Response Format:
{
"_links": {
"_self": "/proxy",
"_parent": "/"
},
"ip": "92.252.241.11",
"port": 4145,
"protocol": "socks4",
"anonymity": "high anonymity",
"lastTested": "2020-06-19 09:55:54",
"allowsRefererHeader": true,
"allowsUserAgentHeader": true,
"allowsCustomHeaders": true,
"allowsCookies": true,
"allowsPost": true,
"allowsHttps": true,
"country": "RU",
"connectTime": "3.312",
"downloadSpeed": "50.000",
"secondsToFirstByte": "3.472",
"uptime": "64.125"
}
To Achieve this Requirement, I Have created a custom Object as Proxy_Server__c and Some Custom Fields to store the Response.
But I'm getting an Error as Compile Error: Incompatible types since an instance of Map<String,Object> is never an instance of List<Object> at line 19 column 34
public class GetProxyDataCallout {
public static HttpResponse makeGetProxyDataCallout(){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api.getproxylist.com/proxy');
request.setMethod('GET');
HttpResponse response = http.send(request);
System.debug(response);
System.debug(response.getBody());
List<Proxy_Server__c> upsertGetProxyList = new List<Proxy_Server__c>();
//Deserialize The JSON Response in to List of Objects
Map<String, Object> results = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());
System.debug(results);
List<Object> proxyList = (List<Object>)results;
System.debug(proxyList);
for(Object proxyListobj : proxyList){
System.debug(proxyListobj);
Map<string, Object> proxyAddress = (Map<string, Object>)proxyListObj;
System.debug(proxyAddress.get('ip'));
Proxy_Server__c proxyIpList = new Proxy_Server__c();
proxyIpList.Name = (String)proxyAddress.get('country');
proxyIpList.IP_Address__c = (String)proxyAddress.get('ip');
proxyIpList.Port__c = (String)proxyAddress.get('port');
proxyIpList.Last_Update__c = (String)proxyAddress.get('lastTested');
proxyIpList.Proxy_Level__c = (String)proxyAddress.get('anonymity');
proxyIpList.Provider__c = 'Get Proxy';
proxyIpList.Proxy_Provider__c = string.isNotBlank(proxyServerId)?proxyServerId:'';
upsertGetProxyList.add(proxyIpList);
}
UPSERT upsertGetProxyList;
return response;
}
}
Please Help me to resolve the issue.
Thanks In Advance.
You could use something like https://json2apex.github.io/ to create a blueprint to deserialize the json response you get.
I hope this helps.
Regards,
Anutej
1. Define the following class based on HTTP response to this class:
2. Use the following line to deserialize http reponse body.
JSONClass myJSONInstance = (JSONClass)JSON.deserialize(response.getBody(), JSONClass.class);
3. Build Proxy_Server__c object data and insert. the reponse does not return map or list at all, not sure why you tried to deserialize http reponse body to map and list.
Proxy_Server__c proxyIpList = new Proxy_Server__c();
proxyIpList.Name = myJSONInstance .country';
proxyIpList.IP_Address__c = myJSONInstance.ip;
proxyIpList.Port__c = myJSONInstance.port;
proxyIpList.Last_Update__c = myJSONInstance.lastTested;
proxyIpList.Proxy_Level__c = myJSONInstance.anonymity;
proxyIpList.Provider__c = 'Get Proxy';
proxyIpList.Proxy_Provider__c = myJSONInstance.proxyServerId;
insert proxyIpList;