``I have a rest service which is reading a json string which is below listed. I am testing this through workbench and i am getting the following error.
Expected JSON object to deserialize apex parameter from at [line:4, column:7] errorCode: JSON_PARSER_ERROR
How do we debug this?
{
"courses":
[
{
"CourseId" : "301",
"CourseName" : "Basic Hardwood Installation"
},
{
"CourseId" : "401",
"CourseName" : "Advanced Hardwood Installation"
}
]
}
Here is my deserialized code. I was wondering whether i can deserialize the entire json in one code or should i be looping to get the value i want?
enter code here
JSONParser parser = JSON.createParser(courses);
while (parser.nextToken() != null) {
// Start at the array of invoices.
if (parser.getCurrentToken() == JSONToken.START_ARRAY) {
while (parser.nextToken() != null) {
// Advance to the start object marker to
// find next invoice statement object.
if (parser.getCurrentToken() == JSONToken.START_OBJECT) {
// Read entire invoice object, including its array of line items.
CoursesWrapper courseWrapperObj = (CoursesWrapper)parser.readValueAs(CoursesWrapper.class);
system.debug('Course Id: ' + courseWrapperObj.courseId);
system.debug('CourseName: ' + courseWrapperObj.courseName);
// For debugging purposes, serialize again to verify what was parsed.
/*String s = JSON.serialize(inv);
system.debug('Serialized invoice: ' + s);
*/
// Skip the child start array and start object markers.
parser.skipChildren();
}
}
}