0

Json to Apex conversion is done and while saving the class in Salesforce ORG throws error.Type cannot be constructed: String

{
"accounts" : {
    "data" : [{
            "access_token" : "EAACEdEostrtre0cBAN0i40rLaxz1E79BH5QngiwStR7DoaQsX7ZAFJ1QLm66fo07W76WYGqU4X1GjC8uqqcGawOA6MBms5gZAfo0VQA4kch3ltoIPd86zBK1ecLzznS5hdTizYZBYqpsIse9jVqA0PZCIIeVR6WAA46sTjJZBW8ZBnqgZDZD",
            "category" : "Organization",
            "name" : "Busyday",
            "id" : "1616378787867178658991",
            "perms" : [
                "ADMINISTER",
                "EDIT_PROFILE",
                "CREATE_CONTENT",
                "MODERATE_CONTENT",
                "CREATE_ADS",
                "BASIC_ADMIN"
            ]
        }
    ],
    "paging" : {
        "cursors" : {
            "before" : "MTYxNjrtrtM2NzE3ODY1ODk5MQZDZD",
            "after" : "MTYxNjMretrt2NzE3ODY1ODk5MQZDZD"
        }
    }
},
"id" : "1150203232221712395"
}

public class facebookpages {
public static void consumeObject(JSONParser parser) {
    Integer depth = 0;
    do {
        JSONToken curr = parser.getCurrentToken();
        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 Accounts accounts {get;set;} 
public String id {get;set;} 

public facebookpages(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 == 'accounts') {
                    accounts = new Accounts(parser);
                } else if (text == 'id') {
                    id = parser.getText();
                } else {
                    System.debug(LoggingLevel.WARN, 'Root consuming unrecognized property: '+text);
                    consumeObject(parser);
                }
            }
        }
    }
}

public class Paging {
    public Cursors cursors {get;set;} 

    public Paging(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 == 'cursors') {
                        cursors = new Cursors(parser);
                    } else {
                        System.debug(LoggingLevel.WARN, 'Paging consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }
}

public class Cursors {
    public String before {get;set;} 
    public String after {get;set;} 

    public Cursors(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 == 'before') {
                        before = parser.getText();
                    } else if (text == 'after') {
                        after = parser.getText();
                    } else {
                        System.debug(LoggingLevel.WARN, 'Cursors consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }
}

public class Data {
    public String access_token {get;set;} 
    public String category {get;set;} 
    public String name {get;set;} 
    public String id {get;set;} 
    public List<String> perms {get;set;} 

    public Data(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 == 'access_token') {
                        access_token = parser.getText();
                    } else if (text == 'category') {
                        category = parser.getText();
                    } else if (text == 'name') {
                        name = parser.getText();
                    } else if (text == 'id') {
                        id = parser.getText();
                    } else if (text == 'perms') {
                        perms = new List<String>();
                        while (parser.nextToken() != JSONToken.END_ARRAY) {
                            perms.add(new String(parser)); // Throws ERROR
                        }
                    } else {
                        System.debug(LoggingLevel.WARN, 'Data consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }
}

public class Accounts {
    public List<Data> data {get;set;} 
    public Paging paging {get;set;} 

    public Accounts(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 == 'data') {
                        data = new List<Data>();
                        while (parser.nextToken() != JSONToken.END_ARRAY) {
                            data.add(new Data(parser));
                        }
                    } else if (text == 'paging') {
                        paging = new Paging(parser);
                    } else {
                        System.debug(LoggingLevel.WARN, 'Accounts consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }
}


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

https://json2apex.herokuapp.com/

1 Answer 1

2

You can't construct a string, with or without parameters. It has no constructors defined. The error tells you exactly what's going wrong. You can never do:

String someText = new String(...);

As for what to do instead, you should be able to use parser.getText() like you do everywhere else. Honestly you should drop the parser altogether and just use serialization. Why reinvent the wheel?

public static FacebookPages parse(String payload) {
    return JSON.deserialize(payload, FacebookPages.class);
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .