软件152 苏锐
在JavaScript中使用:
<script>
function test()
{
//json 对象
var peoper = {
p:[
{"name":"kay","sex":"man","age":"22"},
{"name":"Rose","sex":"girl","age":"21"}
]};
alert(peoper.p[0].name); //value kay
alert(peoper.p[1].sex); //value girl
//修改值
peoper.p[0].age = "55";
peoper.p[0].name = "FanKai";
alert(peoper.p[0].name); //value FanKai
alert(peoper.p[0].age); //value 55
//var ss = peoper.toJSONString();
alert(peoper);
}
</script>
在Action(Servlet)中使用JSON:
String js = request.getParameter("json");
try
{
JSONObject json = new JSONObject(js);
System.out.println("姓名:"+json.getString("name"));
System.out.println("性别:"+json.getString("sex"));
}
catch (JSONException e)
{
e.printStackTrace();
}
给客户端发送JSON:
JSONObject json = new JSONObject();
json.append("name", "kay");
json.append("sex", "boy");
response.getWriter().print(json.toString());