JSON字符串 - 两个以上的参数 - 在servlet上接收null

时间:2017-03-04 10:36:24

标签: javascript java jquery json servlets

我从jQuery传递两个JSON对象 - 使用JSON Stringify对servlet进行ajax调用。但是我变得无效了。如果我传递一个对象,我得到预期的数据,但我无法接收两个对象。请帮我找出错误。

$.ajax({
    url : 'insertserv1',
    type: 'POST',
    dataType: 'json',
    data: JSON.stringify({"test1" :masterdata,"test2" :InspTableArray}),
    contentType: 'application/json',
    mimeType: 'application/json',
    success : function(data) {
        alert('Hi');
    }
});
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));       

if (br != null) {
    json = br.readLine();
}

System.out.println(json); // getting expected data as {"test1":{"grn":"55555","pono":"888888","row":1},"test2":["Type/,"As ","ok","ok","ok","ok","ok"]}

try {
   JSONObject rawdata = new JSONObject(json);
   JSONObject datat1 = rawdata.getJSONObject("test1");
   JSONObject datat2 = rawdata.getJSONObject("test2");

   System.out.println(datat1); // return nulls
   System.out.println(datat2); // return nulls

1 个答案:

答案 0 :(得分:0)

InspTableArray中发送的JSON无效,因此当您尝试在Java中解析时,它会中断:

{"test1":{"grn":"55555","pono":"888888","row":1},"test2":["Type/,"As ","ok","ok","ok","ok","ok"]}
// Problem is this character here    --------------------------^
// which breaks the JSON Array of strings

// The value should be "Type" instead of "Type/
// and is likely causing a SyntaxError: Unexpected token A in JSON at position 66

test2似乎是一个字符串数组,除了第一个不是String的元素并导致数据的其余部分中断并且应该是:

"test2":["Type","As ","ok","ok","ok","ok","ok"]
//            ^------ forward slash replaced with closing double-quotation mark