json数组对像转换成java对象集合
index.jsp页⾯
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd"> <html>
<%String path = ContextPath(); %>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="<%=path%>/content/scripts/json/json2.js"></script>
<script type="text/javascript" src="<%=path%>/content/scripts/jquery/jquery-1.4.2.js"></script>
<script type="text/javascript" src="<%=path%>/content/scripts/jquery/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
/* js对象 */
function sendPerson(name,phone,email){
this.phone=phone;
}
function send(){
//定义js数组
var arrObj = new Array();
/
/创建js对象
for (var i = 0; i < 10; i++) {
var person = new sendPerson(i,i+1,i+2);
arrObj.push(person);
}
if(arrObj.length>0){
var send_obj = JSON.stringify(arrObj);
}
}
function sendUserAjax(send_obj){
$.ajax({
type: "post",
dataType:'json', //接受数据格式
data:"send_obj="+send_obj,
url: "<%=path%>/messageMgt/sendMessage.do",
success: function(data){
$.messager.alert('提⽰','发送成功!');
},
error:function(){
$.messager.alert('提⽰','发送失败!');
}
});
}
</script>
</head>
<body>
<input type="button" value="发送" οnclick="send()" />
</body>
</html>
java后台类
ity;
/**
*发送信息对象
*/
public class MsgSendObj {
private String phone;
private String email;
private String receiverName;
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
}
public String getReceiverName() {
return receiverName;
}
public void setReceiverName(String receiverName) {
}
@Override
public String toString() {
return "MsgSendObj [phone=" + phone + ", email=" + email
+ ", receiverName=" + receiverName + "]";
}
public MsgSendObj(String phone, String email, String receiverName) { super();
this.phone = phone;
}
public MsgSendObj() {
super();
// TODO Auto-generated constructor stub
}
}
控制器中的处理
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import ipfsys.ms.vo.MsgSendObj;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class sendController  extends MultiActionController {
/**
* 发送消息
* @param request
* @param response
*/
public void sendMessage(HttpServletRequest request,HttpServletResponse response){  String send_obj = Parameter("send_obj");
List<MsgSendObj> sendObjList = getDTOArray(send_obj,MsgSendObj.class);
System.out.println(sendObjList);//ava对象集合输出
JSONObject json = JSONObject.fromObject(true);
response.setCharacterEncoding("utf-8");
try {
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/
**
* 将json数组解析成java对象集合
* @param jsonString
* @param clazz
* @return
*/
public static List getDTOArray(String jsonString, Class clazz){
JSONArray array = JSONArray.fromObject(jsonString);
List beans = new ArrayList<>();
for(int i = 0; i < array.size(); i++){
JSONObject jsonObject = JSONObject(i);
beans.add( Bean(jsonObject,clazz));//将建json对象转换为Person对象放到list集合中        }
return beans;
json转换对象
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。