在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>
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:try
{
JSONObject json = new JSONObject(js);
System.out.println("姓名:"+json.getString("name"));
System.out.println("性别:"+json.getString("sex"));
}
catch (JSONException e)
{
e.printStackTrace();
}
JSONObject json = new JSONObject();
json.append("name", "kay");
json.append("sex", "boy");
response.getWriter().print(json.toString());
资料:json.append("name", "kay");
json.append("sex", "boy");
response.getWriter().print(json.toString());
http://www.ibm.com/developerworks/cn/web/wa-ajaxintro10/
http://www.ibm.com/developerworks/cn/web/wa-ajaxintro11.html
http://www.javaeye.com/topic/71343
http://www.json.org/json-zh.html
http://www.javaeye.com/topic/139477