json入门

来源:互联网 发布:防止sql注入 编辑:程序博客网 时间:2024/06/16 01:39

今天看了看我们项目中的json,,,感觉时曾相识 好吧 我就做了个小例子给大家show一下,回顾下

引用json需要的jar包

commons-beanutils-1.7.0.jar

commons-collections-3.2.1.jar

commons-httpclient-3.1.jar

commons-lang-2.3.jar

commons-logging-1.1.1.jar

ezmorph-1.0.3.jar

json-lib-2.2.3-jdk15.jar

这个是action中的小例子

package com.json.test;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


import com.json.bean.StuBean;


public class Json {



public static void JsonBean()
{
StuBean stu=new StuBean();
stu.setId(12);
stu.setName("张三");
stu.setAddress("北京");
JSONObject jo=JSONObject.fromObject(stu);
boolean isArray=jo.isArray();
boolean isempty=jo.isEmpty();
boolean isnullObject=jo.isNullObject();
System.out.println("jo.isArray:"+isArray+"######"+"jo.isempty:"+isempty+"#######"+"jo.isnullObject:"+isnullObject);
System.out.println(jo.toString()+"_____");

StuBean sb=(StuBean) JSONObject.toBean(jo, StuBean.class);

System.out.println(sb.getId()+"$$$$"+sb.getName()+"$$$$"+sb.getAddress());
}

public static void JsonMap()
{
Map map=new HashMap();
StuBean stu1=new StuBean();
stu1.setId(32);
stu1.setName("张三");
stu1.setAddress("襄阳");
StuBean stu2=new StuBean();
stu2.setId(35);
stu2.setName("李四");
stu2.setAddress("北京");
map.put("stu1", stu1);
map.put("stu2", stu2);
map.put("stu3", "value3");
map.put("time", Calendar.getInstance().getTime());
JSONObject jo=JSONObject.fromObject(map);
boolean isArray=jo.isArray();
boolean isempty=jo.isEmpty();
boolean isnullObject=jo.isNullObject();
System.out.println("jo.isArray:"+isArray+"######"+"jo.isempty:"+isempty+"#######"+"jo.isnullObject:"+isnullObject);
System.out.println(jo.toString()+"@@@@@");




}

public static void JsonArray()
{
List arr=new ArrayList();
StuBean stu1=new StuBean();
stu1.setId(32);
stu1.setName("张三");
stu1.setAddress("襄阳");
StuBean stu2=new StuBean();
stu2.setId(35);
stu2.setName("李四");
stu2.setAddress("北京");
arr.add(stu1);
arr.add(stu2);


//注意下如果我们是把ArrayList转Json 要用JSONArray


JSONArray jo=JSONArray.fromObject(arr);
boolean isArray=jo.isArray();
boolean isempty=jo.isEmpty();
boolean isExpandElements=jo.isExpandElements();
System.out.println("jo.isArray:"+isArray+"######"+"jo.isempty:"+isempty+"#######"+"jo.isExpandElements:"+isExpandElements);
System.out.println(jo.toString()+"@@@@@");
}


public static void main(String[] args) {
//JsonBean();
//JsonMap();
JsonArray();
}
}


下面写个json.html 小例子

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>json.html</title>

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

<script type="text/javascript">
var x={'id':"2014",
'name':'HLJW',
works:[{'id':'no1','name':'zhangsan'},
{'id':'no2','name':'lisi'},
{'id':'no3','name':'wangwu'}]};
with(document){
writeln(x.id+"<br/>");
writeln(x.name+"<br/>");
var w=x.works;
for(var i=0;i<w.length;i++)
{
writeln(w[i].id+" : "+w[i].name);
}
}
</script>

  </head>
  
  <body>
    
  </body>
</html>

0 0