package Demo;

 import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Vector; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature; import entity.Userinfo; /**
* fastjson 是一个性能很好的 Java 语言实现的 JSON 解析器和生成器,来自阿里巴巴的工程师开发。 主要特点:
* 1.快速FAST(比其它任何基于Java的解析器和生成器更快,包括jackson) 强大(支持普通JDK类包括任意Java Bean
* 2.Class、Collection、Map、Date或enum) 零依赖(没有依赖其它任何类库除了JDK)
*
*/
public class TestFastJson { public static void main(String[] args) {
String json = "{\"name\":\"chenggang\",\"age\":24}";
String arrayAyy = "[[\'马云',50],null,[\'马化腾',30]]";
// Entity2json("zhangsan", 24);
// list2Json();
Complexdata();
// Deserialization(json);
// DateFormate(new Date());
// Json2Eetity(json);
// String2JSONArray(arrayAyy);
} // 实体转为Json
public static void Entity2json(String name, int age) {
Userinfo info = new Userinfo(name, age);
String str_json = JSON.toJSONString(info); //
System.out.println("实体转化为Json" + str_json);
} // list转Json
public static void list2Json() {
List<Userinfo> list = new ArrayList<Userinfo>();
Userinfo userinfo1 = new Userinfo("lisi", 15);
Userinfo userinfo2 = new Userinfo("wangwu", 16);
list.add(userinfo1);
list.add(userinfo2);
String json = JSON.toJSONString(list, true);
System.out.println("List集合转json格式字符串 :" + json);
} // 字符数组转化为JSon
private static void String2JSONArray(String arrayAyy) {
JSONArray array = JSONArray.parseArray(arrayAyy);
System.out.println("数组:" + array);
System.out.println("数组长度: " + array.size());
Collection nuCon = new Vector();
nuCon.add(null);
array.removeAll(nuCon);
System.out.println("数组:" + array);
System.out.println("数组长度: " + array.size());
} // 复杂数据类型
public static void Complexdata() {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("username", "zhangsan");
map.put("age", 24);
map.put("sex", "男"); // map集合
HashMap<String, Object> temp = new HashMap<String, Object>();
temp.put("name", "xiaohong");
temp.put("age", "23");
map.put("girlInfo", temp); // list集合
List<String> list = new ArrayList<String>();
list.add("爬山");
list.add("骑车");
list.add("旅游");
map.put("hobby", list);
String jsonString = JSON.toJSONString(map);
System.out.println("复杂数据类型:" + jsonString);
} public static void Deserialization(String json) {
Userinfo userInfo = JSON.parseObject(json, Userinfo.class);
System.out.println("姓名是:" + userInfo.getName() + ", 年龄是:"
+ userInfo.getAge());
} // 格式话日期
public static void DateFormate(Date date) {
System.out.println("输出毫秒值:" + JSON.toJSONString(date));
System.out.println("默认格式为:"
+ JSON.toJSONString(date,
SerializerFeature.WriteDateUseDateFormat));
System.out.println("自定义日期:"
+ JSON.toJSONStringWithDateFormat(date, "yyyy-MM-dd",
SerializerFeature.WriteDateUseDateFormat));
} // Json转为实体
private static void Json2Eetity(String json) {
Userinfo userInfo = JSON.parseObject(json, Userinfo.class);
System.out.println("输出对象的地址:" + userInfo.toString());
System.out.println("输出对象的名字:" + userInfo.getName());
}
}

以上Demo所用到的实体类:

 package entity;

 public class Userinfo {
private static final long serialVersionUID = 1L;
private String name;
private int age; public Userinfo() {
super();
} public Userinfo(String name, int age) {
super();
this.name = name;
this.age = age;
} public void setName(String name) {
this.name = name;
} public String getName() {
return name;
} public void setAge(int age) {
this.age = age;
} public int getAge() {
return age;
}
}

最新文章

  1. UINavigationController
  2. CentOS 7部署OpenStack(二)—安装keystone服务
  3. I/O知识
  4. Android属性动画完全解析(上),初识属性动画的基本用法
  5. VPN各种常见状态码及修复方法
  6. APP前端公共测试点
  7. 【Qt】Qt之进程间通信(QProcess)【转】
  8. MicrosoftActiveSync 安装
  9. 如何给Qlabel添加clicked属性(覆盖mousePressEvent,处理QMouseEvent消息,反正是软绘制,想怎么样就怎么样)
  10. Xcode升级7.3 自动补全不提示导入的自定义类解决方案
  11. ORALCE 之LRU链与脏LRU链【转载】
  12. [转载] java垃圾回收机制
  13. 数据库的Connection、Cursor两大对象
  14. dialog记录
  15. 解密gzip压缩的网页数据流(转)
  16. 使用docker-compose创建启动容器时候提示服务名已存在
  17. [eclipse]添加python默认模板,在首行添加编码方式(# -*- coding: utf-8 -*-)
  18. 【ARTS】01_02_左耳听风-20181119~1125
  19. thinkphp自动验证详解
  20. VueRouter

热门文章

  1. iOS 使用NJKWebViewProgress做webview进度条
  2. C - Big Number
  3. mysql+php+pdo批量添加大数据
  4. wamp出现问题#1045 - Access denied for user &#39;root&#39;@&#39;localhost&#39; (using password: NO)的解决方法
  5. URI、URL和URN之间的区别与联系
  6. yii2不用composer使用redis
  7. SharedPreference对象及其xml文件
  8. Java魔法堂:打包知识点之META-INF/MAINFEST.MF(转)
  9. linux之SQL语句简明教程---CREATE TABLE
  10. linux之SQL语句简明教程---GROUP BY