json所依赖的jar包
http://files.cnblogs.com/files/wenjie123/json_jar%E5%8C%85.rar package com.hp.svse; import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale; import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
/**
* Timestamp 处理器
*/
public class JsonDateValueProcessor implements JsonValueProcessor { private String formatDateTime ="yyyy-MM-dd HH:mm:ss";
private String formatDate ="yyyy-MM-dd";
public Object processArrayValue(Object value, JsonConfig config) {
return process(value);
} public Object processObjectValue(String key, Object value, JsonConfig config) {
return process(value);
} private Object process(Object value){ if(value instanceof Date){
SimpleDateFormat sdf ;
if(value.toString().length()<=11||value.toString().indexOf("00:00:00.0")>=0){
sdf= new SimpleDateFormat(formatDate,Locale.UK);
}else{
sdf= new SimpleDateFormat(formatDateTime,Locale.UK);
}
return sdf.format(value);
}
return value == null ? "" : value.toString();
}
}
package com.hp.svse;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig; public class JsonTest {
public static void main(String[] args) { /**
* 将对象集合转换成JSON格式的字符串
*/
List<Student> students = new ArrayList<Student>();
students.add(new Student("小明1", "男", "湖北", "SVSE"));
students.add(new Student("小明2", "女", "广东", "GIS"));
students.add(new Student("小明3", "男", "香港", "3G"));
JsonTest jsonTest = new JsonTest();
String json = jsonTest.beanListToJSON(students);
System.out.println(json);
/**
* 输出结果:
* [
* {"sex":"男","address":"湖北","stuname":"小明1","classname":"SVSE","smallStudents":[]},
* {"sex":"女","address":"广东","stuname":"小明2","classname":"GIS","smallStudents":[]},
* {"sex":"男","address":"香港","stuname":"小明3","classname":"3G","smallStudents":[]}
* ]
*/ /**
* 将json字符串转换成json对象 再讲json对象转换成实体对象bean
*/
String bookCotentjsonStr = "{\"sex\":\"男\",\"address\":\"湖北\",\"stuname\":\"小明1\",\"classname\":\"SVSE\"}";
JSONObject bookCotentjson = JSONObject.fromObject(bookCotentjsonStr);
Student student = (Student) JSONObject.toBean(bookCotentjson, Student.class);
System.out.println(student);
/**
* 输出结果:Student [address=湖北, classname=SVSE, sex=男, stuname=小明1]
*/ /**
* 将对象转换成JSON格式的字符串 适用于单个对象(单个对象里可以包含List<T>)的转换
*/
List<SmallStudent> smallStudents = new ArrayList<SmallStudent>();
for (int i = 0; i < 3; i++) {
SmallStudent smallStudent = new SmallStudent();
smallStudent.setName("小学生"+i);
smallStudent.setAge("1"+i);
smallStudents.add(smallStudent);
}
Student student2 = new Student("小明", "男", "武汉", "svse",smallStudents);
String jsonStr = jsonTest.beanToJSON(student2);
System.out.println(jsonStr);
/**
* 输出结果:
* {"sex":"男","address":"武汉","stuname":"小明","classname":"svse","smallStudents":[{"sex":"","age":"10","name":"小学生0"},{"sex":"","age":"11","name":"小学生1"},{"sex":"","age":"12","name":"小学生2"}]}
*/ }
/**
* 将集合转换成JSON格式的字符串
*/
public <T> String beanListToJSON(List<T> t) {
String json = "";
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(Date.class,
new JsonDateValueProcessor());
if(t!=null){
json = JSONArray.fromObject(t, jsonConfig).toString();
}else{
json="[]";
}
return json;
} /**
* 将对象转换成JSON格式的字符串 适用于单个对象(单个对象里可以包含List<T>)的转换
*/
public <T> String beanToJSON(T t) {
String json = "";
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(Date.class,
new JsonDateValueProcessor());
if(t!=null){
json = JSONObject.fromObject(t, jsonConfig).toString();
}else{
json="[]";
}
return json;
}
}
package com.hp.svse;

import java.util.List;

public class Student {
private String stuname;
private String sex;
private String address;
private String classname;
private List<SmallStudent> smallStudents; public Student(String stuname, String sex, String address, String classname,List<SmallStudent> smallStudents) {
super();
this.stuname = stuname;
this.sex = sex;
this.address = address;
this.classname = classname;
this.smallStudents = smallStudents;
}
public Student(String stuname, String sex, String address, String classname) {
super();
this.stuname = stuname;
this.sex = sex;
this.address = address;
this.classname = classname;
}
public Student() {
super();
} public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getClassname() {
return classname;
}
public void setClassname(String classname) {
this.classname = classname;
} public List<SmallStudent> getSmallStudents() {
return smallStudents;
} public void setSmallStudents(List<SmallStudent> smallStudents) {
this.smallStudents = smallStudents;
} @Override
public String toString() {
return "Student [address=" + address + ", classname=" + classname
+ ", sex=" + sex + ", stuname=" + stuname + "]";
} }
package com.hp.svse;

public class SmallStudent {
private String name;
private String age;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "SmallStudent [age=" + age + ", name=" + name + ", sex=" + sex
+ "]";
}
}

json <--->List集合,实体类 之间的相互转换的更多相关文章

  1. android开发学习 ------- json数据与实体类之间的相互转换

    在网络请求的时候,会返回给我们实体类,我们需要将实体类转化为json字符串,方便处理数据: 有时候也会将json数据转换为实体类. 在Android Studio中,json要互相转换,需要用到gso ...

  2. ASP.NET JSON数据转实体类方式

    实体类 public class FlieList { public string file_unid { get; set; } public string file_name { get; set ...

  3. 【转】C#中将JSon数据转换成实体类,将实体类转换成Json

    http://wo13145219.iteye.com/blog/2022667 http://json2csharp.chahuo.com/ using System; using System.C ...

  4. (转)DATATABLE(DATASET)与实体类之间的互转.

    转自:http://www.cnblogs.com/zzyyll2/archive/2010/07/20/1781649.html dataset和实体类 之间的转换 //dataset转实体类  代 ...

  5. java 使用反射在dto和entity 实体类之间进行转换

    package com.example.demo.utils; import java.lang.reflect.Method; import java.util.List; import com.e ...

  6. ASP.NET自带对象JSON字符串与实体类的转换

    关于JSON的更多介绍,请各位自行google了解!如果要我写的话,我也是去Google后copy!嘿嘿,一直以来很想学习json,大量的找资料和写demo,总算有点了解! 切入正题! 还是先封装一个 ...

  7. JSON.net 在实体类中自定义日期的格式

    定义日期格式转换类,其继承 IsoDateTimeConverter,代码如下: public class DateTimeConverter : IsoDateTimeConverter { pub ...

  8. c# XML和实体类之间相互转换(序列化和反序列化)[砖]

    link: http://blog.okbase.net/haobao/archive/62.html by: 好饱 我们需要在XML与实体类,DataTable,List之间进行转换,下面是XmlU ...

  9. C# XML和实体类之间相互转换(序列化和反序列化)

    我们需要在XML与实体类,DataTable,List之间进行转换,下面是XmlUtil类,该类来自网络并稍加修改. using System; using System.Collections.Ge ...

随机推荐

  1. mysql 连接url中useUnicode=true&characterEncoding=UTF-8 的作用

    添加的作用是:指定字符的编码.解码格式.            例如:mysql数据库用的是gbk编码,而项目数据库用的是utf-8编码.这时候如果添加了useUnicode=true&cha ...

  2. Ubuntu apt-get 更换源,以提高下载速度

    http://blog.csdn.net/gaojinshan/article/details/9244823 Ubuntu的默认源是美国的,所以下载起来特别慢,作为天朝的用户,自然要更换为天朝的源了 ...

  3. AlertDialog.Builder 样式设置

    AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDia ...

  4. php获取html checkbox的值。

    一个小错误,搞了好久: <label><input class="short" type="checkbox" id="is_onl ...

  5. PEP Index > PEP 339 -- Design of the CPython Compiler 译文

    http://www.python.org/dev/peps/pep-0339/ PEP: 339 标题: CPython的编译器设计 版本: 425fc5598ee8 最后修改: 2011-01-1 ...

  6. redis+PHP实现的一个优先级去重队列

    主要思路是用一个set做前端去重缓冲, 若干个list做后端的多优先级消息队列, 用一个进程来进行分发, 即从set中分发消息到队列. set缓冲的设计为当天有效, 所以有个零点问题,有可能在零点前s ...

  7. struts2文件异步上传

    代码结构 用到的js文件: 用到的jar包: jar和js库文件下载地址:下载地址 首先我们写FileAction类 package com.ajaxfile.action; import java. ...

  8. 【CF】7 Beta Round D. Palindrome Degree

    manacher+dp.其实理解manacher就可以解了,大水题,dp就是dp[i]=dp[i>>1]+1如何满足k-palindrome条件. /* 7D */ #include &l ...

  9. Ehcache详细解读(转)

    Ehcache 是现在最流行的纯Java开源缓存框架,配置简单.结构清晰.功能强大,最初知道它,是从Hibernate的缓存开始的.网上中文的EhCache材料 以简单介绍和配置方法居多,如果你有这方 ...

  10. Unity3d 实现顶点动画

    在今年GDC上发现一个非常有趣的演讲,叫做Animating With Math,遂实现之,是讲述顶点shader动画的,举了几个经典的例子,但是讲者并没有给代码,而是像虚幻引擎那样的节点,这样更加清 ...