1

Error:

Method does not exist or incorrect signature: JSON2Apex.parse(String)

I used https://json2apex.herokuapp.com/. I got back APEX and a test class that should work. I am getting the above error. I have added to the controller so perhaps something is butting heads but I am not sure what direction to go in. Can anyone see anything glaringly wrong with this?

Class

public class CalloutAgentList {
        //Public String userEmail{ get; set;}
        public JSON2Apex info { get; set; }
        public static String jsonStr {get;set;} 
        public static Datetime changeToPst {get;set;}
        public static String newDate {get;set;}

    //this should return a JSON string
    public static String getAccounts() {
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://myendpoint.com');
        req.setMethod('GET');
        Http h = new Http();
        string d = getAuth();
        string e = 'token ';
        string f = e + d;

        req.setheader('Authorization', f );
        try{
            HttpResponse res = h.send(req);
            jsonStr = res.getBody();
            return jsonStr;
        }
        catch(Exception z){
            system.debug('this is the system debug for excception z: ' + z);
            return null;
        }
    }
    //authorizing 
    public static string getAuth() {
        try{
            Http h = new Http();
            HttpRequest req = new HttpRequest();
            req.setEndpoint('https://gettingatoken.com');
            req.setMethod('GET');      
            req.setHeader('Authorization', 'apikey 12345678946563211456');
            HttpResponse res = h.send(req);
            System.debug('this is the res: ' + res.getStatusCode() + ', ' + res.getBody());
            return res.getBody();
        }
        catch(Exception q){
            system.debug('this is the system debug for excception q: '+ q);
            return null;
        }
    }
    public static String helloKitty = getAccounts();

    //wrapper class 
    public class JSON2Apex 
    {
        public Datetime DateModifiedUtc{get; set;}
        public List<AgentInfoList> AgentInfoList{get; set;}
    }

    public class AgentInfoList 
    {
        public String Username { get; set; }
        public Integer PriorityNumber { get; set; }
    }

    public static JSON2Apex parse(String json)  
    {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }

    public CalloutAgentList() 
    {
        info = (JSON2Apex)parse(helloKitty); 
        system.debug('this is the string-->' + info);
        system.debug('this is the string-->' + info.AgentInfoList);
        system.debug('this is the string-->' + info.DateModifiedUtc);
        changeToPst = info.DateModifiedUtc;
        System.debug('This is the Converted Time to PST is ' + changeToPst.format('MM/dd/yyyy HH:mm:ss', 'America/Los_Angeles'));
        newDate = changeToPst.format('MM/dd/yyyy hh:mm aa' , 'America/Los_Angeles');
    }
}

Test

static testMethod void testParse() {
    String json = '{\"DateModifiedUtc\":\"2016-09-07T20:12:47.1519119Z\",\"AgentInfoList\":[{\"Username\":\"Agent00@gmail.com\",\"PriorityNumber\":0},{\"Username\":\"Agent01@gmail.com\",\"PriorityNumber\":0},{\"Username\":\"Agent02@gmail.com\",\"PriorityNumber\":0},{\"Username\":\"Agent03@gmail.com\",\"PriorityNumber\":0},{\"Username\":\"Agent04@gmail.com\",\"PriorityNumber\":0},{\"Username\":\"Agent05@gmail.com\",\"PriorityNumber\":0}]}';
    //CalloutAgentList obj = CalloutAgentList.parse(json);
    CalloutAgentList.JSON2Apex obj = CalloutAgentList.parse(json);
    System.assert(obj != null);
}
  • 3
    What's the outer class that wraps your first snippet? That's what you need to reference, not the JSON2Apex inner class. – Adrian Larson Sep 27 '16 at 15:40
  • @AdrianLarson I have tried using the outer class name (CalloutAgentList ) and get a similar error message. See edits. – Olivia Sep 27 '16 at 15:46
  • That should work...Can you edit your post add the test method where you applied that approach and the verbatim error message? – Adrian Larson Sep 27 '16 at 15:47
  • Edited. Perhaps it is the JSON2Apex method that the CalloutAgentList is having trouble reading... the error I am getting with this is: "Method does not exist or incorrect signature: CalloutAgentList.JSON2Apex.parse(String)" – Olivia Sep 27 '16 at 15:51
  • It's supposed to be CalloutAgentList.parse(...). – Adrian Larson Sep 27 '16 at 15:52
2

I guess you are Not using the Wrapper class name while referring the Inner Json2Apex class .

CalloutAgentList.JSON2Apex obj = CalloutAgentList.parse(json);

Using Wrapper Class name to refer your inner Apex class will solve the problem.

  • ohh...I think you are on to something. I do still get an error "Method does not exist or incorrect signature: CalloutAgentList.JSON2Apex.parse(String)" but this is close. – Olivia Sep 27 '16 at 15:48
  • updated code... with respect to your update of code on orignal post . This will work :) – Pranay Jaiswal Sep 27 '16 at 15:51
  • ah great. It was the second JSON2Apex that was erroring out. This worked. Thank you! – Olivia Sep 27 '16 at 15:53
  • Happy to Help :) – Pranay Jaiswal Sep 27 '16 at 16:05
1

You need to reference the top level class, which has the static method you are trying to call. For example, if your code looks like:

public TopLevelClass
{
    public class JSON2Apex
    {
        //...
    }
    //...
    static JSON2Apex parse(String input)
    {
        //...
    }
}

Then in your test, you need to call TopLevelClass.parse(...), not JSON2Apex.parse(...).

TopLevelClass.JSON2Apex instance = TopLevelClass.parse(payload);

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.