博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Json串和java对象进行转时
阅读量:4308 次
发布时间:2019-06-06

本文共 1114 字,大约阅读时间需要 3 分钟。

json-lib-xxx.jar

ezmorph-xxx.jar  //=============>依赖包

JsonConfig config = new JsonConfig();//有选择性的过滤掉一些属性值

 JSONUtils.getMorpherRegistry().registerMorpher( new DateMorpher(new String[] { "yyyy-MM-dd" }));//注册一个json转为java.util.date的日期格式

 JSONObject o = JSONObject.fromObject(jsonString, config);

  if (clazz == null) {
   return (T) JSONObject.toBean(o);
  } else {
   return (T) JSONObject.toBean(o, clazz);
  }

=====================================================================================================

public static void main(String[] args) {

Map map=new HashMap();
map.put("我","妹");
map.put("擦","哇");
map.put("你","呀");
JSONObject json = JSONObject.fromObject(map);
System.out.println(json);
}

輸出的結果 {"我":"妹","擦":"哇","你":"呀"}

然后测试toBean方法的类

import net.sf.json.JSONObject;
public class ToBeanTest {
public static void main(String[] args) {
String json = "{id:'1001',name:'张三',age:'22'}";
Student stu = new Student();
JSONObject obj = JSONObject.fromObject(json);
stu = (Student)JSONObject.toBean(obj, Student.class);
System.out.println(stu);
}
}
输出结果为1001, 张三, 22

转载于:https://www.cnblogs.com/mingtian521/p/3835672.html

你可能感兴趣的文章
How it works(3) Tilestrata源码阅读(A)
查看>>
How it works(12) Tileserver-GL源码阅读(A) 服务的初始化
查看>>
uni-app 全局变量的几种实现方式
查看>>
echarts 为例讲解 uni-app 如何引用 npm 第三方库
查看>>
uni-app跨页面、跨组件通讯
查看>>
springmvc-helloworld(idea)
查看>>
JDK下载(百度网盘)
查看>>
idea用得溜,代码才能码得快
查看>>
一篇掌握python魔法方法详解
查看>>
数据结构和算法5-非线性-树
查看>>
数据结构和算法6-非线性-图
查看>>
数据结构和算法7-搜索
查看>>
数据结构和算法8-排序
查看>>
windows缺少dll解决办法
查看>>
JPA多条件动态查询
查看>>
JPA自定义sql
查看>>
BigDecimal正确使用了吗?
查看>>
joplin笔记
查看>>
JNDI+springmvc使用
查看>>
vue+springboot分页交互
查看>>