Here is the json string I am working with:
{
"sigTemplateId": 1,
"name": "Test Ticket Template",
"groups": "[{\"sigTemplateId\": 1, \"sigTemplateGroupId\": 1, \"name\": \"Group 1\", \"ordinal\": 1}]"
}
To handle this data, it gets sent from my jsp to the servlet. The servlet code looks like this:
Gson gson = new Gson();
if (jsonData != null) {
Type objType = new TypeToken<SigTemplateObj>() {}.getType();
SigTemplateObj sigTemplateToSave = gson.fromJson(jsonData, objType);
}
The issue I'm facing is a java.lang.IllegalStateException: This is not a JSON Array. It occurs when trying to parse the groups array using GSON. The error message states that the DefaultTypeAdapters$CollectionTypeAdapter failed to deserialize the json object as java.util.ArrayList.
Below is the structure of my object classes:
public class SigTemplateObj {
int sigTemplateId;
String dyninkName;
int dyninkFormId;
String name;
//children collections
ArrayList<SigTemplateFieldObj> fields;
ArrayList<SigTemplateGroupObj> groups;
...
}
public class SigTemplateGroupObj {
int sigTemplateGroupId;
int sigTemplateId;
int ordinal;
String name;
...
}
I have tried different approaches to resolve the issue, but I keep encountering the same problem mentioned above.
If you have any suggestions or solutions, please let me know.
Thank you, Eric
Currently, I need to adjust my javascript to correctly interpret the groups array as an array rather than a string. Here's what I'm trying to do:
var testObject = new Object();
testObject.sigTemplateId = 1;
testObject.name = 'Test Ticket Template';
testObject['groups'] = [];
var testGroup = new Object();
testGroup.sigTemplateId = 1;
testGroup.sigTemplateGroupId = 1;
testGroup.name = 'Group 1';
testGroup.ordinal = 1;
testObject.groups.push(testGroup);
var json = JSON.stringify(testObject);