您的位置:首页 > Web前端 > JavaScript

JSON格式化各种数据类型

2013-09-26 14:56 375 查看
JSON格式化各种数据类型

JSON可格式化的数据:

普通的JavaBean(有些特殊处理的不能被格式化)
Map
List

JSON转换所需的jar包:
commons-beanutils.jar
commons-collections-3.2.1.jar
commons-httpclient-3.1.jar
commons-lang-2.4.jar
commons-logging-1.1.jar
ezmorph-1.0.6.jar
json-lib-2.2.3-jdk13.jar

数据格式化为JSONObject:

//建2个简单的javaBean(TextBean.java,TextBean1.java)具体代码如下:
public class TextBean {
private String propertys1;
private String propertys2;
private Date propertys3;
private TextBean1 tb1;
public String getPropertys1() {
return propertys1;
}
public void setPropertys1(String propertys1) {
this.propertys1 = propertys1;
}
public String getPropertys2() {
return propertys2;
}
public void setPropertys2(String propertys2) {
this.propertys2 = propertys2;
}
public Date getPropertys3() {
return propertys3;
}
public void setPropertys3(Date propertys3) {
this.propertys3 = propertys3;
}
public TextBean1 getTb1() {
return tb1;
}
public void setTb1(TextBean1 tb1) {
this.tb1 = tb1;
}
}


public class TextBean1 {
private String p1;
private String p2;
public String getP1() {
return p1;
}
public void setP1(String p1) {
this.p1 = p1;
}
public String getP2() {
return p2;
}
public void setP2(String p2) {
this.p2 = p2;
}
}


//普通JavaBean格式化为JSON格式数据
public static void main(String[] args) {
TextBean tb=new TextBean();
TextBean1 tb1=new TextBean1();
tb1.setP1("tb1111");
tb1.setP2("tb2222");
tb.setTb1(tb1);
tb.setPropertys1("abcdef");
tb.setPropertys2("123456");
tb.setPropertys3(new Date());
JSONObject jsonObj=JSONObject.fromObject(tb);
System.out.println(jsonObj);
}


输出结果:
{"propertys1":"abcdef","propertys2":"123456","propertys3":{"date":26,"day":4,"hours":13,"minutes":35,"month":8,"seconds":56,"time":1380173756175,"timezoneOffset":-480,"year":113},"tb1":{"p1":"tb1111","p2":"tb2222"}}



//Map格式化为JSON格式数据
public static void main(String[] args) {
TextBean tb=new TextBean();
TextBean1 tb1=new TextBean1();
tb1.setP1("tb1111");
tb1.setP2("tb2222");
tb.setTb1(tb1);
tb.setPropertys1("abcdef");
tb.setPropertys2("123456");
tb.setPropertys3(new Date());
Map map=new HashMap();
map.put("textBean", tb);
map.put("type", "map");
JSONObject jsonObj=JSONObject.fromObject(map);
System.out.println(jsonObj);
}


输出结果:

{"type":"map","textBean":{"propertys1":"abcdef","propertys2":"123456","propertys3":{"date":26,"day":4,"hours":13,"minutes":37,"month":8,"seconds":57,"time":1380173877438,"timezoneOffset":-480,"year":113},"tb1":{"p1":"tb1111","p2":"tb2222"}}}

//List格式化为JSON格式数据
public static void main(String[] args) {
TextBean tb=new TextBean();
TextBean1 tb1=new TextBean1();
tb1.setP1("tb1111");
tb1.setP2("tb2222");
tb.setTb1(tb1);
tb.setPropertys1("abcdef");
tb.setPropertys2("123456");
tb.setPropertys3(new Date());
Map map=new HashMap();
map.put("type", "map");
List list=new ArrayList();
list.add(tb);
list.add(tb1);
JSONArray jsonArray=JSONArray.fromObject(list);
System.out.println(jsonArray);
}


输出结果:

[{"propertys1":"abcdef","propertys2":"123456","propertys3":{"date":26,"day":4,"hours":13,"minutes":43,"month":8,"seconds":9,"time":1380174189900,"timezoneOffset":-480,"year":113},"tb1":{"p1":"tb1111","p2":"tb2222"}},{"p1":"tb1111","p2":"tb2222"}]

过滤某个字段:

//过滤掉time和name字段JsonConfig jsonConfig=new JsonConfig();
jsonConfig.setExcludes(new String[]{"time","name"});
JSONArray jsonArray=JSONArray.fromObject(students,jsonConfig);


解决JSON乱码问题和out输出:

//解决JSON乱码问题
request.setCharacterEncoding("UTF-8");
response.setContentType("text/json");
response.setCharacterEncoding("UTF-8");


//页面接收JSON
PrintWriter out=response.getWriter();
out.write(json);
out.flush();


如果转换的时候报错net.sf.json.JSONException: java.lang.reflect.InvocationTargetException,有可能是因为你的JavaBean中有Date类型的字段,解决方法如下:
1.可以过滤掉Date类型的那个字段
2.解决方法:添加自定义的日期格式转化类

自定义的日期格式转化类:

/**
* 可以自己写一个JsonValueProcessor实现类来实现JsonValueProcessor接口
* 实现processArrayValue方法和processObjectValue方法
*/
public class JsonValueProcessorImpl implements JsonValueProcessor {

private String format = "yyyy-MM-dd";

public JsonValueProcessorImpl() {

}

public JsonValueProcessorImpl(String format) {
this.format = format;
}

public Object processArrayValue(Object value, JsonConfig jsonConfig) {
String[] obj = {};
if (value instanceof Date[]) {
SimpleDateFormat sf = new SimpleDateFormat(format);
Date[] dates = (Date[]) value;
obj = new String[dates.length];
for (int i = 0; i < dates.length; i++) {
obj[i] = sf.format(dates[i]);
}
}
return obj;
}

public Object processObjectValue(String key, Object value,
JsonConfig jsonConfig) {
if (value instanceof Date) {
String str = new SimpleDateFormat(format).format((Date) value);
return str;
}
if(null != value){
return value.toString();
}
return "";
}

public String getFormat() {
return format;
}

public void setFormat(String format) {
this.format = format;
}
}


/**
* 然后定义一个JsonConfig,设置JsonConfig的相应属性:
*/
JsonConfig config = new JsonConfig();
config.setExcludes(new String[]{"handler","hibernateLazyInitializer"});		//字段过滤
config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);		//防止自包含
config.registerJsonValueProcessor(java.util.Date.class, new JsonValueProcessorImpl());	//引用日期格式转换类


//转换的时候可以这么调用
JSONArray.fromObject(dutyList,config);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息