0

I am trying to parse my JSON string into salesforce so i can manipulate it and use the information to create some objects. Issue is when I try to use the parse function it isn't mapping and I am getting the error:

Invalid type: JSON2Dir.parse

Any ideas ? As everything I have read this is how it is implemented.

JSON2Dir class

public class JSON2Dir{
    public String etag; 
    public Integer items_per_page;  
    public Integer active_count;    
    public Integer inactive_count;  
    public Integer total_results;   
    public cls_items[] items;
    public cls_links links;
    public Integer start_index; 
    public String kind; 
    public Integer resigned_count;  
    class cls_items {
        public String nationality;  //British
        public cls_address address;
        public String name; //FAIRHURST, Russell William
        public String appointed_on; //2004-01-30
        public String officer_role; //secretary
        public cls_links links;
    }
    class cls_address {
        public String postal_code;  //LU5 5XE
        public String locality; //Dunstable
        public String address_line_1;   //Whitbread Court Houghton Hall
        public String address_line_2;   //Business Park Porz Avenue
        public String region;   //Bedfordshire
    }
    class cls_links {
        public String self; 
    }
    class cls_officer {
        public String appointments; 
    }
    public static JSON2Dir parse(String json){
        return (JSON2Dir) System.JSON.deserialize(json, JSON2Dir.class);
    }

}

PullDirectors class

global with sharing class PullDirectors {

   webservice static String getDirectors(String regNumber) {
    //test
      //C_House CH = new C_House();
      C_House.CompaniesHouseCalloutsResponse CHresp = new C_House.CompaniesHouseCalloutsResponse();
      C_House.CompaniesHouseCalloutsRequest CHreq = new C_House.CompaniesHouseCalloutsRequest();
      C_House.CompaniesHouse.OfficerList CH = new C_House.CompaniesHouse.OfficerList();
      // On success, return a message to the calling program.
      String json;
      json = CHreq.getRequestOfficersList(regNumber);
      JSON2Dir Dir = new JSON2Dir.parse(json); 

      System.debug('CH : ' + test);
      return test;
   }

}

Also Example JSON response:

{"start_index":0,"resigned_count":28,"inactive_count":0,"etag":"af3645083fb8bb104fc42d58e01c9f38be1dcf4c","links":{"self":"/company/01270695/officers"},"items":[{"officer_role":"secretary","appointed_on":"2004-01-30","name":"FAIRHURST, Russell William","links":{"officer":{"appointments":"/officers/Dl5W8mieRiNMJgutiePVDiiQrs0/appointments"}},"nationality":"British","address":{"postal_code":"LU5 5XE","locality":"Dunstable","address_line_1":"Whitbread Court Houghton Hall","address_line_2":"Business Park Porz Avenue","region":"Bedfordshire"}},{"nationality":"British","name":"LOWRY, Daren Clive","links":{"officer":{"appointments":"/officers/7wlTqN4bUrGsviZG7xv5NBTa6ls/appointments"}},"appointed_on":"2004-01-30","officer_role":"secretary","address":{"locality":"Dunstable","postal_code":"LU5 5XE","address_line_2":"Business Park Porz Avenue","address_line_1":"Whitbread Court Houghton Hall","region":"Bedfordshire"}},{"occupation":"Director","address":{"address_line_2":"Business Park Porz Avenue","address_line_1":"Whitbread Court Houghton Hall","locality":"Dunstable","postal_code":"LU5 5XE","region":"Bedfordshire"},"appointed_on":"2013-03-18","date_of_birth":{"month":8,"year":1970},"officer_role":"director","name":"COTTA, Jason","nationality":"British","links":{"officer":{"appointments":"/officers/9QLiPPZgMiOemY2-zQT95lcUFeM/appointments"}},"country_of_residence":"United Kingdom"},{"address":{"address_line_2":"Business Park Porz Avenue","address_line_1":"Whitbread Court Houghton Hall","region":"Bedfordshire","postal_code":"LU5 5XE","locality":"Dunstable"},"occupation":"Finance Director","appointed_on":"2015-04-16","date_of_birth":{"month":7,"year":1978},"country_of_residence":"United Kingdom","nationality":"British","name":"HIGHFIELD, Sarah Louise","links":{"officer":{"appointments":"/officers/MPqE4JO8KUCg2sFtEUFBVRM_-Jo/appointments"}},"officer_role":"director"},{"occupation":"Managing Director","address":{"region":"Bedfordshire","locality":"Dunstable","postal_code":"LU5 5XE","address_line_2":"Business Park Porz Avenue","address_line_1":"Whitbread Court Houghton Hall"},"appointed_on":"2016-06-20","date_of_birth":{"year":1971,"month":5},"officer_role":"director","country_of_residence":"United Kingdom","name":"PAUL, Dominic James","links":{"officer":{"appointments":"/officers/3Wi2Er1pbxypGoi1-_d_C-QfnrY/appointments"}},"nationality":"British"},{"occupation":"Group Hr Director Costa","address":{"region":"Bedfordshire","postal_code":"LU5 5XE","address_line_2":"Business Park Porz Avenue","locality":"Dunstable","address_line_1":"Whitbread Court Houghton Hall"},"appointed_on":"2016-10-13","date_of_birth":{"year":1972,"month":3},"officer_role":"director","country_of_residence":"United Kingdom","nationality":"British","name":"SELJEFLOT, Katherine Joanna","links":{"officer":{"appointments":"/officers/G_ZupARFe09AWuuqRZ1gYfqdR4o/appointments"}}},{"officer_role":"secretary","appointed_on":"1997-05-27","name":"BUXTON SMITH, Maria Rita","links":{"officer":{"appointments":"/officers/Z9uebB15Cy64dVLYqqbCJh87mSE/appointments"}},"resigned_on":"2002-09-25","address":{"region":"Bucks","locality":"Newport Pagnell","postal_code":"MK16 8RH","address_line_1":"83 Wordsworth Avenue"}},{"links":{"officer":{"appointments":"/officers/iltSHba44-20R1ZekmECvGC2O2A/appointments"}},"name":"COSTA, Yolanda","officer_role":"secretary","address":{"region":"Surrey","locality":"Purley","postal_code":"CR8 3LJ","address_line_1":"The Copse 9 Rose Walk"},"resigned_on":"1995-10-01"},{"resigned_on":"1995-10-09","address":{"postal_code":"W1Y 2AD","locality":"London","address_line_1":"78 Brook Street"},"appointed_on":"1995-10-01","officer_role":"secretary","links":{"officer":{"appointments":"/officers/iHLK-qzk42T_JiXxz-ZAzoflteQ/appointments"}},"name":"D & H B SECRETARIAL SERVICES LIMITED"},{"appointed_on":"1995-10-09","officer_role":"secretary","name":"FENTON, Nicola Jane","links":{"officer":{"appointments":"/officers/R38gj8kZIH2Rajt9GgdbrMmyUK8/appointments"}},"resigned_on":"1997-05-27","address":{"locality":"Hampstead","postal_code":"NW3 1ND","region":"London","address_line_1":"Flat 1","address_line_2":"56 Rosslyn Hill"}},{"resigned_on":"2004-01-30","address":{"address_line_1":"85 Silverdale Road","postal_code":"RG6 7NF","locality":"Reading","region":"Berkshire","address_line_2":"Earley"},"appointed_on":"2002-09-25","officer_role":"secretary","name":"THORPE, Elizabeth Anne","links":{"officer":{"appointments":"/officers/0NWwYUDiU7C83qk0fzbXvlFZ0EQ/appointments"}}},{"name":"BENNETT, Joanna Mary","nationality":"British","links":{"officer":{"appointments":"/officers/3dqCBv6IQwSc5vJ3Q-oa9gmgF9I/appointments"}},"country_of_residence":"United Kingdom","officer_role":"director","date_of_birth":{"year":1959,"month":1},"resigned_on":"2016-10-13","appointed_on":"2014-02-13","address":{"locality":"Dunstable","postal_code":"LU5 5XE","address_line_1":"Whitbread Court Houghton Hall","address_line_2":"Business Park Porz Avenue","region":"Bedfordshire"},"occupation":"Group Hr Director Costa"},{"date_of_birth":{"month":3,"year":1967},"resigned_on":"2017-01-31","nationality":"British","name":"BENTLEY, Clive","links":{"officer":{"appointments":"/officers/25a4yRmVgnRPa6xxSjgcBlJ61zc/appointments"}},"officer_role":"director","address":{"address_line_1":"Whitbread Court Houghton Hall","region":"Bedfordshire","postal_code":"LU5 5XE","address_line_2":"Business Park Porz Avenue","locality":"Dunstable"},"occupation":"Property Director","appointed_on":"2009-01-26"},{"date_of_birth":{"year":1955,"month":3},"resigned_on":"1997-02-24","country_of_residence":"United Kingdom","nationality":"British","name":"CLEWLEY, Robert Richard","links":{"officer":{"appointments":"/officers/PuZMrfk8eLuLEuj0vHKGMb5FJHw/appointments"}},"officer_role":"director","address":{"address_line_2":"Tonbridge Road","address_line_1":"Sunfield House","region":"Kent","locality":"Ightham","postal_code":"TN15 9AR"},"occupation":"Executive","appointed_on":"1995-10-09"},{"nationality":"Italian","name":"COSTA, Sergio Giorgio","links":{"officer":{"appointments":"/officers/jOAO2Ih0DUi9H-uZSGcpjZevcN4/appointments"}},"officer_role":"director","date_of_birth":{"year":1949,"month":4},"resigned_on":"1995-10-09","address":{"postal_code":"MC 98000","locality":"Monte Carlo","country":"Monaco","address_line_1":"1254 Palais De La Scala","address_line_2":"1 Avenue Henry Dunant"},"occupation":"Director"},{"address":{"region":"Surrey","postal_code":"CR8 3LJ","locality":"Purley","address_line_1":"The Copse 9 Rose Walk"},"occupation":"Director","links":{"officer":{"appointments":"/officers/iltSHba44-20R1ZekmECvGC2O2A/appointments"}},"name":"COSTA, Yolanda","nationality":"Italian","officer_role":"director","date_of_birth":{"year":1948,"month":11},"resigned_on":"1995-10-01"},{"address":{"address_line_2":"Business Park Porz Avenue","address_line_1":"Whitbread Court Houghton Hall","postal_code":"LU5 5XE","locality":"Dunstable","region":"Bedfordshire"},"occupation":"Managing Director","appointed_on":"2006-06-13","date_of_birth":{"month":12,"year":1956},"resigned_on":"2012-07-19","country_of_residence":"United Kingdom","nationality":"British","name":"DERKACH, John","links":{"officer":{"appointments":"/officers/LesY2peG92IexlgcW7RKACsG-z0/appointments"}},"officer_role":"director"},{"date_of_birth":{"year":1955,"month":8},"resigned_on":"2001-05-04","country_of_residence":"United Kingdom","nationality":"British","name":"DOWELL, Michael Charles Weston","links":{"officer":{"appointments":"/officers/0QQu4mCL5i4pi3X4o-m1ecj4ozA/appointments"}},"officer_role":"director","address":{"postal_code":"CM22 7HQ","region":"Hertfordshire","country":"England","address_line_1":"High Street","address_line_2":"Hatfield Broad Oak","locality":"Bishops Stortford","premises":"Post House"},"occupation":"Managing Director","appointed_on":"1996-12-01"},{"appointed_on":"2001-04-02","occupation":"Solicitor","address":{"locality":"London","address_line_2":"Muswell Hill","postal_code":"N10 2RD","address_line_1":"37 Windermere Road"},"officer_role":"director","nationality":"British","name":"FAIRHURST, Russell William","links":{"officer":{"appointments":"/officers/Dl5W8mieRiNMJgutiePVDiiQrs0/appointments"}},"country_of_residence":"England","resigned_on":"2001-04-18","date_of_birth":{"month":9,"year":1963}},{"appointed_on":"2003-10-28","address":{"address_line_2":"Ealing","address_line_1":"16 Lindfield Road","locality":"London","postal_code":"W5 1QR"},"occupation":"Accountant","nationality":"British","name":"FOWLER, James Dominic","links":{"officer":{"appointments":"/officers/rdGN4IH4fKWNIp1YC8_7O7K1GSw/appointments"}},"officer_role":"director","date_of_birth":{"month":7,"year":1967},"resigned_on":"2004-09-20"},{"country_of_residence":"United Kingdom","links":{"officer":{"appointments":"/officers/DzVPRbue2HbbNfFPYLzCtdeoXC0/appointments"}},"name":"HARDY, Helen","nationality":"British","officer_role":"director","date_of_birth":{"year":1961,"month":3},"resigned_on":"2014-02-13","appointed_on":"2010-03-08","address":{"address_line_1":"Whitbread Court Houghton Hall","address_line_2":"Business Park Porz Avenue","locality":"Dunstable","postal_code":"LU5 5XE","region":"Bedfordshire"},"occupation":"Director"},{"officer_role":"director","links":{"officer":{"appointments":"/officers/aW6-l8iXGFIzTp4XjkqhbxaJRJY/appointments"}},"name":"JOHNSON, Adrian James","nationality":"British","resigned_on":"2013-01-25","date_of_birth":{"year":1964,"month":2},"appointed_on":"2008-06-27","occupation":"Chief Operating Officer","address":{"region":"Bedfordshire","address_line_2":"Business Park Porz Avenue","address_line_1":"Whitbread Court Houghton Hall","locality":"Dunstable","postal_code":"LU5 5XE"}},{"resigned_on":"1999-02-18","date_of_birth":{"month":3,"year":1956},"officer_role":"director","name":"JOHNSON, Michael Andrew","nationality":"British","links":{"officer":{"appointments":"/officers/_F4DnVVc7aX5UGuP6Cn2kLPi6SE/appointments"}},"country_of_residence":"United Kingdom","occupation":"Managing Director Chain Restau","address":{"postal_code":"SO51 0LQ","region":"Hampshire","address_line_2":"Oakley Road","locality":"Mottisfont","address_line_1":"North End House"},"appointed_on":"1995-10-09"},{"occupation":"Finance Director","address":{"address_line_2":"Mill Hill","locality":"London","address_line_1":"36 Copthall Drive","postal_code":"NW7 2NB"},"appointed_on":"2000-08-10","resigned_on":"2002-02-01","date_of_birth":{"month":12,"year":1957},"officer_role":"director","country_of_residence":"United Kingdom","name":"MANSIGANI, Mohan","nationality":"British","links":{"officer":{"appointments":"/officers/yxpcYReKM47U-HMuQgC4P1CZNrU/appointments"}}},{"officer_role":"director","country_of_residence":"United Kingdom","name":"MARSHALL, Andrew John","links":{"officer":{"appointments":"/officers/omTiawqphNVwyGVpuv2BgRVEaQE/appointments"}},"nationality":"British","resigned_on":"2015-06-15","date_of_birth":{"year":1960,"month":12},"appointed_on":"2010-03-08","occupation":"Director","address":{"address_line_2":"Business Park Porz Avenue","address_line_1":"Whitbread Court Houghton Hall","locality":"Dunstable","postal_code":"LU5 5XE","region":"Bedfordshire"}},{"occupation":"Director","address":{"postal_code":"LU5 5XE","locality":"Dunstable","region":"Bedfordshire","address_line_1":"Whitbread Court Houghton Hall","address_line_2":"Business Park Porz Avenue"},"appointed_on":"2015-04-16","resigned_on":"2017-04-30","date_of_birth":{"year":1977,"month":4},"officer_role":"director","country_of_residence":"United Kingdom","links":{"officer":{"appointments":"/officers/_vAHtXSZUbR7t_236JZme4UX9wU/appointments"}},"name":"MCGOWAN, Murray Henry","nationality":"British"},{"appointed_on":"2002-01-01","address":{"address_line_1":"Scotts Cottage","address_line_2":"Scots Lane","locality":"Adstock","postal_code":"MK18 2HX","region":"Buckinghamshire"},"occupation":"Accountant","name":"PHILLIPS, Mark Randall","links":{"officer":{"appointments":"/officers/64LGut81iaUXAJERs09m2-rYBYE/appointments"}},"nationality":"British","country_of_residence":"United Kingdom","officer_role":"director","date_of_birth":{"year":1957,"month":11},"resigned_on":"2006-06-30"},{"address":{"postal_code":"LU5 5XE","address_line_1":"Whitbread Court Houghton Hall","region":"Bedfordshire","locality":"Dunstable","address_line_2":"Business Park Porz Avenue"},"occupation":"Company Director","appointed_on":"2009-01-26","date_of_birth":{"year":1969,"month":7},"resigned_on":"2014-03-18","name":"PRICE, Matthew John","nationality":"British","links":{"officer":{"appointments":"/officers/gNE6orzOz2z-ctT_TTLkOba5BB0/appointments"}},"officer_role":"director"},{"country_of_residence":"England","links":{"officer":{"appointments":"/officers/EbDCCHed3gqOjZfyLs63nZTTcJw/appointments"}},"name":"ROGERS, Christopher Charles Bevan","nationality":"British","officer_role":"director","date_of_birth":{"month":4,"year":1960},"resigned_on":"2016-06-20","appointed_on":"2012-07-06","address":{"address_line_1":"Whitbread Court Houghton Hall","postal_code":"LU5 5XE","region":"Bedfordshire","locality":"Dunstable","address_line_2":"Business Park Porz Avenue"},"occupation":"Director"},{"officer_role":"director","nationality":"British","name":"SIMPSON, Richard Barry","links":{"officer":{"appointments":"/officers/CYAhSoQ6VG1P1fhYV6m0hArmwPA/appointments"}},"country_of_residence":"United Kingdom","resigned_on":"2000-08-10","date_of_birth":{"year":1948,"month":12},"appointed_on":"1995-10-09","occupation":"Finance Director","address":{"region":"Northamptonshire","locality":"Northampton","address_line_2":"53 Dallington Road","postal_code":"NN5 7BW","address_line_1":"Astra House"}},{"appointed_on":"2010-03-08","occupation":"Director","address":{"postal_code":"LU5 5XE","address_line_2":"Business Park Porz Avenue","address_line_1":"Whitbread Court Houghton Hall","locality":"Dunstable","region":"Bedfordshire"},"officer_role":"director","country_of_residence":"England","name":"SLATER, Kenneth James","nationality":"British","links":{"officer":{"appointments":"/officers/70y_lTDL2max6QWtrYkJONZTeHA/appointments"}},"resigned_on":"2015-11-19","date_of_birth":{"month":7,"year":1969}},{"appointed_on":"2001-07-01","occupation":"Company Director","address":{"region":"Hertfordshire","postal_code":"HP4 2RQ","address_line_2":"Frithsden Copse Potten End","locality":"Berkhamsted","address_line_1":"Gade House"},"officer_role":"director","nationality":"British","name":"TYE, Michael Edward","links":{"officer":{"appointments":"/officers/k05BCLqlBlstvitdXJCYd9ZIRnQ/appointments"}},"country_of_residence":"United Kingdom","resigned_on":"2003-10-28","date_of_birth":{"month":12,"year":1953}},{"appointed_on":"2004-09-20","occupation":"Director","address":{"postal_code":"E1W 3SJ","address_line_1":"61 Wapping Wall","premises":"36 Merchant Court","locality":"London"},"officer_role":"director","country_of_residence":"United Kingdom","name":"WOODHOUSE, Loraine","nationality":"British","links":{"officer":{"appointments":"/officers/CUH3MULCwb4jfkAyqgEkCyPB2HU/appointments"}},"resigned_on":"2008-06-27","date_of_birth":{"year":1968,"month":12}},{"date_of_birth":{"year":1957,"month":6},"resigned_on":"2001-11-30","links":{"officer":{"appointments":"/officers/otps14yX4edHMXJVYbcZlCbXMhw/appointments"}},"name":"WRIGHT, Nicholas David","nationality":"British","officer_role":"director","address":{"address_line_1":"Kilby House","address_line_2":"Danesbury Park Road","locality":"Welwyn Garden City","postal_code":"AL6 9SF","region":"Herts"},"occupation":"Finance Director","appointed_on":"2001-07-30"}],"kind":"officer-list","items_per_page":35,"total_results":34,"active_count":6}
1

in order to call static method of the class, you need to use ClassName.staticMethodName. keyword new means, creating new instance of class. In this example you have static method parse of JSON2Dir class. So you need to remove new word.

JSON2Dir Dir = JSON2Dir.parse(json);

1
  • You should explain why "new" shouldn't be used.
    – sfdcfox
    Jun 20 '17 at 20:06

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.