0

**I am Accessing the Class using postMan I am Writing the Code like

@RestResource(urlMapping='/create Event2/')
global class JSON2Apex {
   @Httppost
   global  static void consumeObject(JSONParser parser) {

It is Showing Error like

Error: Compile Error: Invalid type for Http* method: System.JSONParser at line 6 column 24

How to Rectify the Error**

This is My Entire Code:

@RestResource(urlMapping='/create Event2/')

 global class JSON2Apex {
@Httppost

   global  static void consumeObject(JSONParser parser) {
    system.debug(parser);
        Integer depth = 0;
        do {
            JSONToken curr = parser.getCurrentToken();
            system.debug(curr);
            if (curr == JSONToken.START_OBJECT || 
                curr == JSONToken.START_ARRAY) {
                depth++;
            } else if (curr == JSONToken.END_OBJECT ||
                curr == JSONToken.END_ARRAY) {
                depth--;
            }
        } while (depth > 0 && parser.nextToken() != null);
    }


    public List<Eventitems> eventitems {get;set;} 

    public JSON2Apex(JSONParser parser) {
        while (parser.nextToken() != JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                String text = parser.getText();
                if (parser.nextToken() != JSONToken.VALUE_NULL) {
                    if (text == 'eventitems') {
                        eventitems = new List<Eventitems>();
                        while (parser.nextToken() != JSONToken.END_ARRAY) {
                            eventitems.add(new Eventitems(parser));
                        }
                    } else {
                        System.debug(LoggingLevel.WARN, 'Root consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }

    public class Eventitems {
        public String whatid {get;set;} 
        public String whoid {get;set;} 
        public String OwnerId {get;set;} 
        public Boolean IsAllDayEvent {get;set;} 
        public String Subject {get;set;} 
        public String StartDate {get;set;} 
        public String EndDate {get;set;} 
        public Boolean Recurrence {get;set;} 
        public String Recurrencestdate {get;set;} 
        public String Recurrenceenddate {get;set;} 
        public String RecType {get;set;} 
        public String Recintrvl {get;set;} 
        public List<String> Invitees {get;set;} 

        public Eventitems(JSONParser parser) {
            while (parser.nextToken() != JSONToken.END_OBJECT) {
                if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                    String text = parser.getText();
                    if (parser.nextToken() != JSONToken.VALUE_NULL) {
                        if (text == 'whatid') {
                            whatid = parser.getText();
                        } else if (text == 'whoid') {
                            whoid = parser.getText();
                        } else if (text == 'OwnerId') {
                            OwnerId = parser.getText();
                        } else if (text == 'IsAllDayEvent') {
                            IsAllDayEvent = parser.getBooleanValue();
                        } else if (text == 'Subject') {
                            Subject = parser.getText();
                        } else if (text == 'StartDate') {
                            StartDate = parser.getText();
                        } else if (text == 'EndDate') {
                            EndDate = parser.getText();
                        } else if (text == 'Recurrence') {
                            Recurrence = parser.getBooleanValue();
                        } else if (text == 'Recurrencestdate') {
                            Recurrencestdate = parser.getText();
                        } else if (text == 'Recurrenceenddate') {
                            Recurrenceenddate = parser.getText();
                        } else if (text == 'RecType') {
                            RecType = parser.getText();
                        } else if (text == 'Recintrvl') {
                            Recintrvl = parser.getText();
                        } else if (text == 'Invitees') {
                            Invitees = new List<String>();
                            while (parser.nextToken() != JSONToken.END_ARRAY) {
                              //Invitees.add(new String(parser));
                            }
                        } else {
                            System.debug(LoggingLevel.WARN, 'Eventitems consuming unrecognized property: '+text);
                            consumeObject(parser);
                        }
                    }
                }
            }
        }
    }

    public static JSON2Apex parse(String json) {
        return new JSON2Apex(System.JSON.createParser(json));
    }

}
5
  • have you created any parser class ?
    – Ratan Paul
    Commented Jul 5, 2016 at 6:21
  • Yes How to use Post Annotation in Rest Class
    – user31717
    Commented Jul 5, 2016 at 6:23
  • ahh looks like you are not clear how to use Json2Apex generated class. look the generated class you need to save in new class. then when you get the JSON in consumeObject method use that class (Json2Apex generated class) for deserialize. check this ceus-now.com/how-to-deserialize-a-json-string-to-apex
    – Ratan Paul
    Commented Jul 5, 2016 at 6:31
  • Please Explain Clearly I am Not Understand
    – user31717
    Commented Jul 5, 2016 at 6:32
  • Share the Answer
    – user31717
    Commented Jul 5, 2016 at 6:37

1 Answer 1

2

How to deserialize a JSON String to Apex

Use http://json2apex.herokuapp.com/ to generate you class

and save that class in your salesforce org

now in your rest service class

@RestResource(urlMapping='/create Event2/')
global class MyService {
    @HttpPost
    global static void consumeObject() {
        JSON2Apex objJSON = JSON2Apex.parse(RestContext.request.requestBody.toString());
    }
}

This will deserialize your JSON

2
  • Above Code we have to write in Seperate Rest Service Class ah?
    – user31717
    Commented Jul 5, 2016 at 6:45
  • @Salesforce yes man
    – Ratan Paul
    Commented Jul 5, 2016 at 6:47

You must log in to answer this question.