java数据转json数据

来源:互联网 发布:java 获取本地路径 编辑:程序博客网 时间:2024/05/20 12:24
   在我们使用easyui的时候常常需要json数据,静态的固定的数据还好,一旦是动态的就头疼了。这里就需要我们能熟练的转换java数据。

准备工作:

将json-lib-2.4JAR包解压后包下的所有jar包,copy到WEB-INF下的lib下


json数据的转化:

1、实体类的转换

新建一个User类
idnamepwdroleintstringstringString

<span style="font-size:18px;">User user=new User();user.setId(1);user. setName("mmo");user.setPwd( "123");//没定义role则对应输出为“”JSONObject jsonObject=JSONObject.fromObject(user);String data=jsonObject.toString();System. out.println("实体类数据" +data);</span>


输出结果:
实体类数据{"id":1,"name":"mmo","pwd":"123","role":""}

2、List数据的转换
<span style="font-size:18px;">User user2 =new User();user2.setId(2);user2.setName("lili");user2.setPwd("abc");List<User> list= new ArrayList<User>();list.add(user);list.add( user2);JSONArray jsonArray=JSONArray.fromObject(list);String data_arr=jsonArray.toString();System. out.println("list数据" +data_arr);</span>



输出结果:
list数据[{"id":1,"name":"mmo","pwd":"123","role":""},{"id":2,"name":"lili","pwd":"abc","role":""}]


3、自定义Json数据
<span style="font-size:18px;">JSONObject jObj= new JSONObject();jObj.put( "total", 2);jObj.put( "row", list);System. out.println("自定义" +jObj.toString());</span>



输出结果:
自定义{"total":2,"row":[{"id":1,"name":"mmo","pwd":"123","role":""},{"id":2,"name":"lili","pwd":"abc","role":""}]}

4、Map数据的转化
<span style="font-size:18px;">Map<String, String> map=new HashMap<String, String>();map.put( "id", "1002" );map.put( "name", "lily" );JSONObject jsonObj2=JSONObject.fromObject(map);System. out.println("map数据" +jsonObj2.toString());</span>


输出结果:
map数据{"id":"1002","name":"lily"}

但是你会发现当你的数据中含有日期等特殊类型的数据时,以上的示例就不怎么有用了
因为它只对基本类型做了定义,日期它根本不知道该怎么转换。因此,你必须得告诉它怎么转

<span style="font-size:18px;">JSONObject jObj=new JSONObject();JsonConfig jsonConfig=new JsonConfig();jObj.put("total",list.size());jsonConfig.registerJsonValueProcessor(Date.class,new DateJsonValueProcessor());jObj.elementOpt("rows",list,jsonConfig);</span>
其中jsonConfig.registerJsonValueProcessor(Date.class,new DateJsonValueProcessor());中的Date.class是它原本的类型+".class",而DateJsonValueProcessor()就是自定义的转换方式了。意思就是当遇到data类型数据时,按DateJsonValueProcessor()里的规定转化

定义一个类实现JsonValueProcessor接口,在
private Object process(Object value) {}方法中写转换方式以下以日期为例:




<span style="font-size:18px;">public class DateJsonValueProcessor implements JsonValueProcessor {private String format = "yyyy-MM-dd";      private SimpleDateFormat sdf = new SimpleDateFormat(format); public Object processArrayValue(Object value, JsonConfig config) {// TODO Auto-generated method stubreturn this.process(value);}public Object processObjectValue(String key, Object value, JsonConfig config) {// TODO Auto-generated method stubreturn this.process(value);} private Object process(Object value) {    if(value== null){        return"";    }else if (value instanceof Date)    return sdf.format((Date)value);    else if(value instanceof Date[]){    Date[] dates=(Date[])value;        String[] obj =new String[dates.length];        for (int i = 0; i < dates.length; i++) {      obj[i]=sdf.format(dates[i]);       }        return obj;        }else{      return value.toString();   }}}</span>




最后就是你要怎么把它和easyui联系起来:
在以上转换了数据之后,在其后加上一句
response.getWriter().println(jObj.toString());(这里的jObj是之前的JSONObject对象或JSONArray对象
 一般在需要使用json的控件上它都有一个url属性,例:url:'abc.json'
这时你只需要把abc.json换为你进行了数据转换的页面地址就可以了

0 0
原创粉丝点击