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. 【 HDU 1255】 覆盖的面积(矩阵面积交,线段树,扫描法)

    [题目] 覆盖的面积 Problem Description 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input 输入数据的第一行是一个正整数T(1<=T<=100 ...

  2. 【网络流24题】No.18 分配问题 (二分图最佳匹配 费用流|KM)

    [题意] 有 n 件工作要分配给 n 个人做.第 i 个人做第 j 件工作产生的效益为 cij . 试设计一个将n 件工作分配给 n 个人做的分配方案, 使产生的总效益最大. 输入文件示例input. ...

  3. UVA 1513 Movie collection

    #include<stdio.h> #include<string.h> #include<stdlib.h> #define N 200010 #define l ...

  4. 【Spring】Spring IOC原理及源码解析之scope=request、session

    一.容器 1. 容器 抛出一个议点:BeanFactory是IOC容器,而ApplicationContex则是Spring容器. 什么是容器?Collection和Container这两个单词都有存 ...

  5. Android用户界面 UI组件--ImageView及其子类ImageButton,QuickContactBadge附带Draw9Patch工具说明

    1.ImageView 常用属性: android:src 设置可绘制对象作为 ImageView 显示的内容 android:cropToPadding 如果设置为true,图片裁剪到保留该Imag ...

  6. jQuery对象和DOM对象原来不一样啊

    jQuery对象和DOM对象使用说明,需要的朋友可以参考下.1.jQuery对象和DOM对象第一次学习jQuery,经常分辨不清哪些是jQuery对象,哪些是 DOM对象,因此需要重点了解jQuery ...

  7. 关于ArrayList线程安全解决方案

    一:使用synchronized关键字 二:使用Collections.synchronizedList();使用方法如下: 假如你创建的代码如下:List<Map<String,Obje ...

  8. Nginx 做负载均衡的几种轮询策略

    网上看见nginx的upstream目前支持的5种方式的分配,摘录备忘. 1.轮询(默认)每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除.upstream back ...

  9. 理解runtime system

    最近需要编译不同平台的库,因此比以前只开发C++程序关注底层更多点.先来看看术语runtime system的解释. 主要参考资料: http://en.wikipedia.org/wiki/Runt ...

  10. cf754D

    题意:给你一个数m,有多少优惠券,给个n,主角想用多少优惠券.然后接下来时m行,每行两个数,那张优惠券的优惠区间a,b(在a号货物到b号货物之间的所有都可以优惠) 问你,能不能用k张优惠券,是他的优惠 ...