1.我们先将数据封装成json数组。
jsonstr = "[]";
jsonarray = eval('('+jsonstr+')');
for(var i=0;i<changeclass.length;i++){
var jsonTemp = { "name" :changeclass[i]};
jsonarray.push(jsonTemp);
};
2.将json数组以ajax的方式发送到后台,取的时候直接为request.getParameter("js");
$(function(){
$.ajax({
url:"/xiaje/Ajaxcourseservlet?rightchange=yes",
type:"post",
dataType:"json",
data:{"js":JSON.stringify(jsonarray)},
success:function(data){
}
});
3.将string数据转为json。首先需要导入包fastjson-1.2.51.jar,json-org.jar,fastjson-1.2.51.jar下载地址json-org.jar下载地址(注意JSONObject包得导它import com.alibaba.fastjson.JSONObject;)
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf8");
String strjson=request.getParameter("js");
List<Integer> list = new ArrayList<>();
JSONArray jsonArray = null;
try {
jsonArray = new JSONArray(strjson);
for (int i=0;i<jsonArray.length();i++){
String string = jsonArray.getString(i);
JSONObject jsonObject = JSONObject.parseObject(string);
String name = jsonObject.getString("name");
list.add(Integer.parseInt(name));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}