+ Start a Discussion

unexpected token: JSONGenerator

Getting this error (Error: Compile Error: unexpected token: JSONGenerator at line 11 column 11) when saving a new class, any thoughts? 
Ankit AroraAnkit Arora
Code snippet would be helpful here, are you doing like this?

// Pass true to the constructor for pretty print formatting.
JSONGenerator gen = JSON.createGenerator(true);
// Write data to the JSON string.
gen.writeNumberField('abc', 1.21);
gen.writeStringField('def', 'xyz');
gen.writeObjectField('aaa', intlist);
gen.writeFieldName('Object A');

// Get the JSON string.
String pretty = gen.getAsString();

I'm working through the integration workbook.  Here's the code snippet:
JSONGenerator gen = JSON.createGenerator(true);
        // open the JSON generator
        // interate through the list of invoices passed in to the call
        // writing each invoice Id to the array
        for (Id invoiceId : invoiceIds) {
            gen.writeStringField('id', invoiceId);
        // close the JSON generator
        // create a string from the JSON generator
        String jsonOrders = gen.getAsString();
        // debugging call, which you can check in console logs
        System.debug('jsonOrders: ' + jsonOrders);
David "w00t!" LiuDavid "w00t!" Liu
The only reference to JSONGenerator I see in your code is the first line, but supposedly the error is on line 11!

The problem could be from code that you have before this line, if any (ie missing semi-colon in the line before)?  Is there any code that you're not posting?
Robin AlmoesRobin Almoes
I have had the same error while going through the workbook and found what caused it. Just pasting it here for people having the same problem.
Above the code in the workbook is a button to copy the code to your clipboard. If you use that to copy the code from the workbook and paste it into your Apes class, it will NOT paste correctly.
The solution is to copy the code by selecting it in its entirety. Then it will paste correctly.