function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sidbsidb 

JSONParser Error

Hi

I am writing a JSON Parser Apex class and following the documentation. I get the following error:
Error: YelpAPIController Compile Error: Illegal assignment from JSONParser to JSONParser at line 56 column 10

Here is my code:

public class JSONParserUtil {
@future(callout=true)
public static void parseJSONResponse() {
Http httpProtocol = new Http();
// Create HTTP request to send. 

HttpRequest request = new HttpRequest();
// Set the endpoint URL. 

String endpoint = 'http://www.cheenath.com/tutorial/sfdc/sample1/response.php';
request.setEndPoint(endpoint);
// Set the HTTP verb to GET. 

request.setMethod('GET');
// Send the HTTP request and get the response. 

// The response is in JSON format. 

HttpResponse response = httpProtocol.send(request);
System.debug(response.getBody());

// Parse JSON response to get all the totalPrice field values. 

JSONParser parser = JSON.createParser(response.getBody());
Double grandTotal = 0.0;
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
(parser.getText() == 'totalPrice')) {
// Get the value. 

parser.nextToken();
// Compute the grand total price for all invoices. 

grandTotal += parser.getDoubleValue();
}
}
system.debug('Grand total=' + grandTotal);
}
}




I am not sure what wrong here. The error is not explaining anything.

Thanks
Sid


SuperfellSuperfell

Errors like this are normally beause you have a class with the same name, do you have a class called JSONParser ?

Tomasz Piechota 6Tomasz Piechota 6
I had the same problem with JSON2Apex generated class
public class BookingsWrapper {

public class Miscellaneous {
	public Integer id {get;set;} 
	public List<Place> place {get;set;} 
	public String resort {get;set;} 
	public String name {get;set;} 
	public String information {get;set;} 
	public List<CreditCards> customFields {get;set;} 

	public Miscellaneous(JSONParser parser) {
		while (parser.nextToken() != System.JSONToken.END_OBJECT) {
			if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
				String text = parser.getText();
				if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
					if (text == 'id') {
						id = parser.getIntegerValue();
					} else if (text == 'place') {
						place = arrayOfPlace(parser);
					} else if (text == 'resort') {
						resort = parser.getText();
					} else if (text == 'name') {
						name = parser.getText();
					} else if (text == 'information') {
						information = parser.getText();
					} else if (text == 'customFields') {
						customFields = arrayOfCreditCards(parser);
					} else {
						System.debug(LoggingLevel.WARN, 'Miscellaneous consuming unrecognized property: '+text);
						consumeObject(parser);
					}
				}
			}
		}
	}
}

[...]

public BookingsWrapper(JSONParser parser) {
	while (parser.nextToken() != System.JSONToken.END_OBJECT) {
		if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
			String text = parser.getText();
			if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
				if (text == 'channel') {
					channel = parser.getIntegerValue();
				} else if (text == 'language') {
					language = parser.getText();
				} else if (text == 'timeStamp') {
					timeStamp = parser.getText();
				} else if (text == 'success') {
					success = parser.getBooleanValue();
				} else if (text == 'bookings') {
					bookings = arrayOfBookings(parser);
				} else {
					System.debug(LoggingLevel.WARN, 'BookingsWrapper consuming unrecognized property: '+text);
					consumeObject(parser);
				}
			}
		}
	}
}	

public static BookingsWrapper parse(String json) {
	System.JSONParser parser = System.JSON.createParser(json);
	return new BookingsWrapper(parser);
}


private static List<CreditCards> arrayOfCreditCards(System.JSONParser p) {
    List<CreditCards> res = new List<CreditCards>();
    if (p.getCurrentToken() == null) p.nextToken();
    while (p.nextToken() != System.JSONToken.END_ARRAY) {
        res.add(new CreditCards(p));
    }
    return res;
}


I also had the following exceptions

Constructor not defined: [Object].<Constructor>(System.JSONParser)
Method does not exist or incorrect signature: void nextToken() from the type JsonParser

So to resolve that issue I added System. to every sub class constructor which was missing after JSON2Apex conversion, ie:
Before
public Miscellaneous(JSONParser parser) {...}
public BookingsWrapper(JSONParser parser) {...}
After:
public Miscellaneous(System.JSONParser parser) {...}
public BookingsWrapper(System.JSONParser parser) {...}
Hope that helps.
Tomasz Piechota 6Tomasz Piechota 6
In your case
try replacing 
JSONParser parser = JSON.createParser(response.getBody());
With
System.JSONParser parser = System.JSON.createParser(response.getBody());

Your code (just to make it readable)
public class JSONParserUtil {

	@future(callout=true)
	public static void parseJSONResponse(){
	
		Http httpProtocol = new Http();

		// Create HTTP request to send.
		HttpRequest request = new HttpRequest();
		
		// Set the endpoint URL.
		String endpoint = 'http://www.cheenath.com/tutorial/sfdc/sample1/response.php';	
		request.setEndPoint(endpoint);
		
		// Set the HTTP verb to GET.
		request.setMethod('GET');
		
		// Send the HTTP request and get the response.
		// The response is in JSON format.
		HttpResponse response = httpProtocol.send(request);
		
		System.debug(response.getBody());
		
		// Parse JSON response to get all the totalPrice field values.		
		JSONParser parser = JSON.createParser(response.getBody());
		
		Double grandTotal = 0.0;
		
		while (parser.nextToken() != null) {
			if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'totalPrice')) {
			
				// Get the value.
				parser.nextToken();
				
				// Compute the grand total price for all invoices.
				grandTotal += parser.getDoubleValue();
			}
		} 
		
		System.debug('Grand total=' + grandTotal);
	
	}
}