最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - parsing JSON array gives error - Stack Overflow

programmeradmin2浏览0评论

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\"]"}"

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\"]"}"
Share edited Mar 8, 2018 at 11:27 Adder 5,8882 gold badges34 silver badges60 bronze badges asked Aug 21, 2014 at 10:22 uylmzuylmz 1,5622 gold badges26 silver badges49 bronze badges 3
  • Yes: your "args" property is a string, not an array – Maurice Perry Commented Aug 21, 2014 at 12:23
  • What do you exactly mean by it? – uylmz Commented Aug 21, 2014 at 12:33
  • The value starts with a quote, not a bracket – Maurice Perry Commented Aug 21, 2014 at 12:45
Add a ment  | 

2 Answers 2

Reset to default 2

Normal JavaScript arrays are designed to hold data with numeric indexes. Try using Object instead of an array.

Try using the below code for constructing the object and check the output :

var o = {};           // Object
o['username'] = 'username';
o['args'] = [];          // Array
o['args'].push('1');
o['args'].push('2');
o['args'].push('3');
var json = JSON.stringify(o);
alert(json);

I think you have one too many quotes in your stringify result. When I construct the object like this:

 var o = {
            username: "username",
            args: ["1","2","3"]
        };

the result from calling JSON.stringify(o)

is this

"{\"username\":\"username\",\"args\":[\"1\",\"2\",\"3\"]}"

notice there are no quotes around my square brackets.

发布评论

评论列表(0)

  1. 暂无评论