My json string is:
{
"sigTemplateId": 1,
"name": "Test Ticket Template",
"groups": "[{\"sigTemplateId\": 1, \"sigTemplateGroupId\": 1, \"name\": \"Group 1\", \"ordinal\": 1}]"
}
This gets sent from my jsp to the servlet. Servlet code is:
Gson gson = new Gson();
if (jsonData != null) {
Type objType = new TypeToken<SigTemplateObj>() {}.getType();
SigTemplateObj sigTemplateToSave = gson.fromJson(jsonData, objType);
//SigTemplateObj sigTemplateToSave = gson.fromJson(jsonData, SigTemplateObj.class);
}
I get by: java.lang.IllegalStateException: This is not a JSON Array. gson trying to parse the groups array: The JsonDeserializer com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@1f4db697 failed to deserialize json object "[{\"sigTemplateId\": 1, \"sigTemplateGroupId\": 1, \"name\": \"Group 1\", \"ordinal\": 1}]" given the type java.util.ArrayList
My object code is:
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 both the code above and this line as well and got the same results both times.
SigTemplateObj sigTemplateToSave = gson.fromJson(jsonData, SigTemplateObj.class);
Any help would be appreciated. Thanks, Eric
Now I just have to fix my javascript to treat the groups array as an array and not a string:
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);