public static void jSONObjectToJavaBean() throws ClassCastException{
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", 1);
jsonObject.put("name", "杨文培");
jsonObject.put("sex", "男");
jsonObject.put("age", null);//如果是null字符串应该表示为"'null'";int时null默认为0
jsonObject.put("birthday", "1993-01-23");
List<String> list = new ArrayList<String>();
list.add("篮球");
list.add("乒乓球");
list.add("羽毛球");
jsonObject.put("hobby", list);
jsonObject.put("boo", false);
System.out.println("jsonObject.toString()="+jsonObject.toString());
try{
Student student = (Student) JSONObject.toBean(jsonObject,Student.class);
System.out.println("student="+student);
}catch(ClassCastException exception){
System.err.println("exception="+exception.getMessage());
}
}

map转换:

     public static void mapToJSONObject(){
Map<String, Object> map = new HashMap<String,Object>();
map.put("id", 1);
map.put("name", "杨文培");
map.put("sex", null);
map.put("age", 23);
map.put("birthday", "1993-01-23");
List<String> list = new ArrayList<String>();
list.add("篮球");
list.add("乒乓球");
list.add("羽毛球");
map.put("hobby", list);
map.put("boo", false);
JSONObject jsonObject = JSONObject.fromObject(map);
System.out.println("map.toString()="+map.toString());
System.out.println("jsonObject.toString()="+jsonObject.toString());
try{
Student student = (Student) JSONObject.toBean(jsonObject,Student.class);
System.out.println("student="+student);
}catch(ClassCastException exception){
System.err.println("exception="+exception.getMessage());
}
}

json转换为javabean的更多相关文章

  1. json、javaBean、xml互转的几种工具介绍

    json.javaBean.xml互转的几种工具介绍 转载至:http://blog.csdn.net/sdyy321/article/details/7024236 工作中经常要用到Json.Jav ...

  2. json、javaBean、xml互转的几种工具介绍 (转载)

    工作中经常要用到Json.JavaBean.Xml之间的相互转换,用到了很多种方式,这里做下总结,以供参考. 现在主流的转换工具有json-lib.jackson.fastjson等,我为大家一一做简 ...

  3. 如何快速通过json构建javabean对象(Intellij IDEA-->GsonFormat使用教程)

    和第三方对接的时候,返回给我们的json时参数字段多是很常见的现象,所以我们手动去创建javabean肯定是要花费不少时间,博主在网上找到了很多种,可用通过json自动生成javabean的工具,这里 ...

  4. Json与javaBean之间的转换工具类

    /**  * Json与javaBean之间的转换工具类  *  * {@code 现使用json-lib组件实现  *    需要  *     json-lib-2.4-jdk15.jar  * ...

  5. json转换为键值对辅助类

    /// <summary> /// json转换为键值对辅助类 /// </summary> public class JsonParser { private static ...

  6. 字符串json转换为xml xml转换json

    原文:字符串json转换为xml xml转换json // To convert an XML node contained in string xml into a JSON string XmlD ...

  7. Visual Studio 2015 将json转换为实体类

    最新写的一个接口需要接收json参数,然后序列化为实体类然后再进行后面的逻辑处理.因为json中键值对比较多,逐一去手写实体中的每个属性太麻烦,于是寻思是否有这样的工具可以将json转换为实体类. 经 ...

  8. 关于flexjson将json转为javabean的使用

    关于flexjson将json转为javabean的使用 import java.sql.Timestamp; import java.util.Date; import flexjson.JSOND ...

  9. Json、JavaBean、String等互转

    Json.JavaBean.String等互转 本文介绍简单的Json.JavaBean.String互换(下文JavaBean简称Object对象,这里不是很严谨) 转换关系如下: 其中String ...

随机推荐

  1. [转载]BT656/BT601/BT1120协议

     [转载] BT656/BT601/BT1120协议以及DM365/DM355/DM6467上使用的YUV颜色空间说明   ITU-R BT.601和ITU-RBT.656国际电信联盟(Interna ...

  2. linux 中printf的使用

    linux 中printf的使用printf "helloworld\n"printf 中换行必须加上\n printf '%d %s\n' 1 "abc" c ...

  3. windows环境下局域网内无法访问apache站点

    DocumentRoot "D:/wamp/www/" <Directory />     AllowOverride none     order deny,allo ...

  4. How to Failover the ‘Cluster Group’

    If you have more than two nodes in the cluster, you can specify the destination node with the follow ...

  5. android学习笔记56——Service

    Service四大组件之一,需要在AndroidMainfest.xml中添加相关配置,运行于后台,不与用户进行交换,没有UI... 配置时可通过<intent-filter.../>元素 ...

  6. Charles 3.11.5 绿色特别版

    Charles 3.11.5 绿色特别版,注册码在key文本中 链接: http://pan.baidu.com/s/1hsoFcyS 密码: q6e1

  7. javascript性能优化总结一(转载人家)

    一直在学习javascript,也有看过<犀利开发Jquery内核详解与实践>,对这本书的评价只有两个字犀利,可能是对javascript理解的还不够透彻异或是自己太笨,更多的是自己不擅于 ...

  8. 字符串连接,数字tostring,写入文件

    #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int x=2410; in ...

  9. GDI+系列

    1.GDI+的概述 2.绘图表面 3.GDI+坐标系 4.用Pen对象画图 1.使用GDI+画线 2.使用GDI+画弧线 3.使用GDI+画曲线 4.使用GDI+画椭圆 5.使用GDI+画矩形.多边形 ...

  10. mysql关于or的索引问题

    摘自: http://www.educity.cn/wenda/590849.html http://blog.csdn.net/hguisu/article/details/7106159 问: 不 ...