Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
error

We made a wrong turn. Try again.

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.
2 answers
  1. Jun 19, 2020, 4:08 PM
    You may follow the code snippet below.

    1. Define the following class based on HTTP response to this class:

    public with sharing class JSONClass {

    public String ip {get;set;}

    public Integer port {get;set;}

    public String protocol {get;set;}

    public String anonymity {get;set;}

    public String lastTested {get;set;}

    public Boolean allowsRefererHeader {get;set;}

    public Boolean allowsUserAgentHeader {get;set;}

    public Boolean allowsCustomHeaders {get;set;}

    public Boolean allowsCookies {get;set;}

    public Boolean allowsPost {get;set;}

    public Boolean allowsHttps {get;set;}

    public String country {get;set;}

    public String connectTime {get;set;}

    public String downloadSpeed {get;set;}

    public String secondsToFirstByte {get;set;}

    public String uptime {get;set;}

    }

    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;

     
0/9000