javaWeb之JSON数据转换

javaWeb之JSON数据转换
Json 格式两种:
1.javascript中的对象 {属性1:值1, 属性2:值2}
2.数组 [value1, value2, value3]
二者可以结合,形式千变万化
javaScript中对象的定义;
var person  = {
name : "feng",
age : 21,
              show : function() {
       alert("sx");
        }

在javaweb中进行json数据转换工具:json-lib.jar
需要的jar包:
                  commons-beanutils.jar
        commons-collections.jar       
commons-lang.jar
commons-logging.jar
ezmorph-1.0.6.jar
json-lib-2.2.3-jdk15.jar

----------------------------------------------------------------------------------------------------------
封装的数据类型分为如下两类:
1.数组,List集合使用的是  JsonArray的fromObject方法
2.对象 使用JSONObject

1.数组,List集合使用的是  JsonArray的fromObject方法
	@Test
	public void study1() {
		//将List<Bean>转换为json 数组
		Product p1 = new Product(1,"电视", 23.4);
		Product p2 = new Product(2,"空调", 34.5);
		List<Product> list = new ArrayList<Product>();	
		list.add(p1);
		list.add(p2);
		//将list集合转换为json
		JSONArray jsonArray = JSONArray.fromObject(list);
		System.out.println(jsonArray.toString());
	}

@Test
	public void study2() {
		//将java数组转换为json
		String[] str = new String[]{"11","aa", "dd"};
		JSONArray jsonArray = JSONArray.fromObject(str);
		
		System.out.println(jsonArray.toString());
	}


2.对象 使用JSONObject

@Test
	public void study3() {
		//将java对象转换为json
		Product p = new Product(2,"空调", 34.5);
		JSONObject jsonObject = JSONObject.fromObject(p);
		
		System.out.println(jsonObject.toString());
	}

@Test
	public void study4() {
		//将json数据转换为javaBean
		String json = "{\"id\":2,\"name\":\"空调\",\"price\":34.5}";
//		JSONObject.fromObject(json);
		Product p = (Product) JSONObject.toBean(JSONObject.fromObject(json), Product.class);
		
		System.out.println(p.getPrice());
	}

json 特殊处理
 1.不想显示JavaBean中的某个字段 转换的json
@Test
	public void study5() {
		//将json数据 中不需要显示的想移除
		
		//JsonConfig config = new JsonConfig();

		//config.setExcludes(new String[] { "count" ,"id"});
		Product p = new Product(2,"空调", 34.5);
		JsonConfig config = new JsonConfig();
		config.setExcludes(new String[] {"id"});
		JSONObject jsonObject = JSONObject.fromObject(p,config);
		
		System.out.println(jsonObject.toString());
	
	}

2.java中的时间类型转换成json
第一步需要定义类型转化器:
package com.json;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

public class JsonDateValueProcessor implements JsonValueProcessor  {
	private String format = "yyyy-MM-dd";
	public JsonDateValueProcessor() {  
        super();  
    }  
      
    public JsonDateValueProcessor(String format) {  
        super();  
        this.format = format;  
    }  
  
    @Override  
    public Object processArrayValue(Object paramObject,  
            JsonConfig paramJsonConfig) {  
        return process(paramObject);  
    }  
  
    @Override  
    public Object processObjectValue(String paramString, Object paramObject,  
            JsonConfig paramJsonConfig) {  
        return process(paramObject);  
    }  
      
      
    private Object process(Object value){  
        if(value instanceof Date){    
            SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.CHINA);    
            return sdf.format(value);  
        }    
        return value == null ? "" : value.toString(); 
    }
}

第二部进行注册:
JsonConfig jsonConfig = new JsonConfig();  
		jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());

演示:
	@Test
	public void study6() {
		//将json数据 中时间进行转换
		Product p = new Product(12,"电脑", 2323);
		
		JsonConfig jsonConfig = new JsonConfig();  
		jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
		JSONObject jsonObject = JSONObject.fromObject(p, jsonConfig);
	
		System.out.println(jsonObject.toString());
	
	
	}



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值