I have the following Javascript object:
var o = {
"username":"username",
"args": [
"1", "2", "3"
]
};
And send it like:
xhr.send(JSON.stringify(o));
My java class:
public class Command implements Serializable {
private String username;
private String[] args;
//getters, setters constructors etc.
}
And in my servlet:
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response){
Command c;
try {
c = gson.fromJson(request.getReader(), Command.class);
} catch(Exception e) {
.
.
.
Gives the error: Expected BEGIN_ARRAY but was STRING at line 1 column X
, where column number X is where the "[ appears in stringified JSON.
From what I understand this should be a very simple and straightforward thing. What am I doing wrong?
EDIT: I think it may be related to JSON.stringify()
behavior with Javascript arrays of strings.
JSON.stringify(o)
returns:
"{"username":"username","args":"[\"1\", \"2\", \"3\"]"}"