Using $.getJSON my result can be empty, a single value or a list of values.
If the result contains a single value i can access the data via result.someReturnedValue however if the result contains a list i have to loop through the result data using array syntax to access the data E.g. result[i].someReturnedValue
Now i dont want to write the same code twice to handle each type of result. (I’m appending the result to DOM elements and the code is getting large when i have to account for both notations) Is there a generic way i can handle the result to prevent this duplication?
Example of single result;
$('div[id="'+divId+'"]').append("<td>" + result.someValue1+ "</td>");
$('div[id="'+divId+'"]').append("<td>" + result.someValue2+ "</td>");
$('div[id="'+divId+'"]').append("<td>" + result.someValue2+ "</td>");
Example of multiple results:
$('div[id="'+divId+'"]').append("<td>" + result[i].someValue1+ "</td>");
$('div[id="'+divId+'"]').append("<td>" + result[i].someValue2+ "</td>");
$('div[id="'+divId+'"]').append("<td>" + result[i].someValue2+ "</td>");
Ok i found a solution for this issue. You can create your own Provider class which Jersey will use when generating the JSON response.
This provider uses the NATURAL JSON notation, which serializes the object as required in list format.
Ensure Spring is aware of the class by including the necessary annotations/bean definition