1

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);
1
  • Your JS code produces valid JSON for me. I pasted it to Chrome's console, and took a console.log(json). Commented Jan 16, 2013 at 19:09

1 Answer 1

3

The groups array is not an array, but a string:

"groups": "..."

Try this JSON instead:

{
    "sigTemplateId": 1,
    "name": "Test Ticket Template",
    "groups": [{"sigTemplateId": 1, "sigTemplateGroupId": 1, "name": "Group 1", "ordinal": 1}]
}
Sign up to request clarification or add additional context in comments.

4 Comments

I changed my string and that worked, but how would I change my javascript so that it doesn't treat it as a string, but as an array?
Javascript: 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);
Please update your question and insert your JS code there. And add the javascript tag to your question.
I found that my question was already answered with a compatibility issue between prototype and JSON libraries. The fix is to delete Array.prototype.toJSON;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.