ajax访问json数据的属性值
问题:怎样取出json数据的某个属性值,比如我弹出框之弹出第一个对象的userName。
import
import
import java.util.ArrayList;
import java.util.List;
import javax.json.JsonArray;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pojo.Student;
public class TestJsonServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter pw=resp.getWriter();
List<Student> list=getList();
String json=JSON.toJSONString(list);//将list转换成json
pw.println(json);
pw.close();
}
public List<Student> getList(){
Student st1=new Student("张","男","666","324@);
Student st2=new Student("王","女","123","324@);
Student st3=new Student("何","女","456","324@);
Student st4=new Student("贾","男","999","324@);
List<Student> list=new ArrayList<>();
list.add(st1);
list.add(st2);
list.add(st3);
list.add(st4);
return list;
}
}
<html>
<head>
<base href="<%=basePath%>">
<script type="text/javascript" src="js/jquery-2.0.0.min.js"></script>
<script type="text/javascript">
function test(){
$.ajax({
url:'testJson',
type:'post',
data:'json',
success:function(data){
alert(data.userName); //弹出的是defined,这个地方应该怎么写。
}
});
}
</script>
</head>
<body>
<input type="button" value="注册" onclick="test();"/>
</body>
</html>