0

I am getting an error while converting the response from JSON to Apex class.

Invalid conversion from runtime type MapAPIDemo.resourceSets to List<MapAPIDemo.resourceSets>

Here is the code below

Apex Code

public with sharing class MapAPIDemo {

    public list<resourceSets> listresourceSet{get;set;}
    public Integer statusCode{get;set;}
    public String copyright{get;set;}
    public String statusDescription{get;set;}
    public String brandLogoUri{get;set;}
    public String traceId{get;set;}
    public String authenticationResultCode{get;set;}
   
   
   
   
    public static void getPostalCodeDetails(String postalCode){
        HttpRequest request = new HttpRequest();
        String endPoint = 'http://dev.virtualearth.net/REST/v1/Locations?postalCode=';
        endPoint += postalCode;
        endPoint += '&key=';
        endPoint += SYSTEM.Label.BING_API;
        request.setEndPoint(endPoint);
        request.setMethod('GET');
        Http http = new Http();
        HttpResponse response = http.send(request);
        if(response.getStatusCode() == 200){
            System.debug('Code Results=====>'+response.getStatus());
            String responseBody = response.getBody();
            //List<MapAPIDemo.resourceSets> lstResource = (List<MapAPIDemo.resourceSets>) JSON.deserialize(response.getBody(),MapAPIDemo.resourceSets.class);

            //system.debug('resourceSets====>'+lstResource);

        }

        

    }

   


    public class resourceSets{
        public list<resources> resources{get;set;}
        public Integer estimatedTotal{get;set;}
    }

    public class resources{
        public String entityType{get;set;}
        public String confidence{get;set;}
        public point point{get;set;}
        public address address{get;set;}
        public String name{get;set;}
        public list<geocodePoints> geocodePoints{get;set;}
        public list<String> bbox{get;set;}
        public list<String> matchCodes{get;set;}
        public String locType{get;set;}
    }

    public class point{
        public list<String> coordinates{get;set;}
        public String type{get;set;}
    }


    public class geocodePoints{
        public String type{get;set;}
        public String calculationMethod{get;set;}
        public list<String> usageTypes{get;set;}
        public list<String> coordinates{get;set;}
    }

    public class address{
        public String postalCode{get;set;}
        public String countryRegion{get;set;}
        public String formattedAddress{get;set;}
        public String adminDistrict2{get;set;}
        public String locality{get;set;}
        public String adminDistrict{get;set;}
    }
}

Thanks for the help,

1
  • 1
    Please provide the json your trying to parse
    – nchursin
    Aug 2 '20 at 13:38
1

From given error -

Invalid conversion from runtime type MapAPIDemo.resourceSets to List<MapAPIDemo.resourceSets>

You are not using list type in JSON.deserialize method.

Please try with this code part -

List<MapAPIDemo.resourceSets> lstResource = (List<MapAPIDemo.resourceSets>) JSON.deserialize(response.getBody(), List<MapAPIDemo.resourceSets>.class);
4
  • Thanks for the help. I am getting an error now System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set Aug 2 '20 at 12:16
  • 1
    @ShantanuMahajan That means your JSON isn't a list, but rather a single object.
    – sfdcfox
    Aug 2 '20 at 13:42
  • 1
    Ysr, please use please instead of pls. Some people may consider it unprofessional, and one of our core tenants is that we want to be taken as if we were professional.
    – sfdcfox
    Aug 2 '20 at 13:49
  • @sfdcfox Thank you for the feedback. I have updated the answer and I will take care in future as well. :)
    – Ysr Shk
    Aug 2 '20 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.