1

I am Writing the Code Like

else if (text == 'Invitees') {
    Invitees = new List < String > ();
    while (parser.nextToken() != JSONToken.END_ARRAY) {
        Invitees.add(new string(parser));
    }

while saving

Error: Compile Error: Type cannot be constructed: string last line

How to Rectify the Error?

My Entire code:

public class JSON2Apex {
public static void consumeObject(JSONParser parser) {
    system.debug(parser);
        Integer depth = 0;
        do {
            JSONToken curr = parser.getCurrentToken();
            system.debug(parser.getCurrentToken());
            system.debug(curr);
            system.debug(JSONToken.START_OBJECT);
            system.debug(JSONToken.START_ARRAY);
            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;} 

        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));
    }

}
  • 1
    what you expect from this new string(parser) ? there is no such constructor in String class – highfive Jul 5 '16 at 9:24
  • How to Rectify,Please Guide the Answer – user31717 Jul 5 '16 at 9:27
  • no problem.​​​​‌​‌​‌​‌​ – martin Jul 5 '16 at 9:44
0

The getText() method can be used to get a string from an instance of a System.JSONParser class. Specifically,

Invitees.add(new string(parser));

should be replaced with

Invitees.add(parser.getText());
| improve this answer | |

Your Answer

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