java对象与json对象之间互相转换实现方法示例

 更新时间:2017年10月20日 09:58:21   作者:sjiang  
这篇文章主要介绍了java对象与json对象之间互相转换实现方法,结合实例形式分析了java对象与json对象相互转换实现步骤与相关操作技巧,需要的朋友可以参考下

本文实例讲述了java对象与json对象之间互相转换实现方法。分享给大家供大家参考,具体如下:

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class MainClass {
  public static void main(String[] args) {
    TestJsonBean();
    TestJsonAttribute();
    TestJsonArray();
  }
  @SuppressWarnings("rawtypes")
  private static void TestJsonArray() {
    Student student1 = new Student();
    student1.setId(1);
    student1.setName("jag");
    student1.setSex("man");
    student1.setAge(25);
    student1.setHobby(new String[]{"篮球","游戏"});
    Student student2 = new Student();
    student2.setId(2);
    student2.setName("tom");
    student2.setSex("woman");
    student2.setAge(23);
    student2.setHobby(new String[]{"上网","跑步"});
    List<Student> list = new ArrayList<Student>();
    list.add(student1);
    list.add(student2);
    JSONArray jsonArray = JSONArray.fromObject(list);
    System.out.println(jsonArray.toString());
    JSONArray new_jsonArray=JSONArray.fromObject(jsonArray.toArray());
    Collection java_collection=JSONArray.toCollection(new_jsonArray);
    if(java_collection!=null && !java_collection.isEmpty())
    {
      Iterator it=java_collection.iterator();
      while(it.hasNext())
      {
        JSONObject jsonObj=JSONObject.fromObject(it.next());
        Student stu=(Student) JSONObject.toBean(jsonObj,Student.class);
        System.out.println(stu.getName());
      }
    }
  }
  private static void TestJsonAttribute() {
    /**
     * 创建json对象并为该对象设置属性
     */
    JSONObject jsonObj = new JSONObject();
    jsonObj.put("Int_att",25);//添加int型属性
    jsonObj.put("String_att","str");//添加string型属性
    jsonObj.put("Double_att",12.25);//添加double型属性
    jsonObj.put("Boolean_att",true);//添加boolean型属性
    //添加JSONObject型属性
    JSONObject jsonObjSon = new JSONObject();
    jsonObjSon.put("id", 1);
    jsonObjSon.put("name", "tom");
    jsonObj.put("JSONObject_att",jsonObjSon);
    //添加JSONArray型属性
    JSONArray jsonArray = new JSONArray();
    jsonArray.add("array0");
    jsonArray.add("array1");
    jsonArray.add("array2");
    jsonArray.add("array3");
    jsonObj.put("JSONArray_att", jsonArray);
    System.out.println(jsonObj.toString());
    System.out.println("Int_att:"+jsonObj.getInt("Int_att"));
    System.out.println("String_att:"+jsonObj.getString("String_att"));
    System.out.println("Double_att:"+jsonObj.getDouble("Double_att"));
    System.out.println("Boolean_att:"+jsonObj.getBoolean("Boolean_att"));
    System.out.println("JSONObject_att:"+jsonObj.getJSONObject("JSONObject_att"));
    System.out.println("JSONArray_att:"+jsonObj.getJSONArray("JSONArray_att"));
  }
  /**
   * java对象与json对象互相转换
   */
  private static void TestJsonBean() {
    /**
     * 创建java对象
     */
    Student student = new Student();
    student.setId(1);
    student.setName("jag");
    student.setSex("man");
    student.setAge(25);
    student.setHobby(new String[]{"篮球","上网","跑步","游戏"});
    /**
     * java对象转换成json对象,并获取json对象属性
     */
    JSONObject jsonStu = JSONObject.fromObject(student);
    System.out.println(jsonStu.toString());
    System.out.println(jsonStu.getJSONArray("hobby"));
    /**
     * json对象转换成java对象,并获取java对象属性
     */
    Student stu = (Student) JSONObject.toBean(jsonStu, Student.class);
    System.out.println(stu.getName());
    /**
     * 创建json对象
     */
    JSONObject jsonObj = new JSONObject();
    jsonObj.put("id",1);
    jsonObj.put("name","张勇");
    jsonObj.put("sex","男");
    jsonObj.put("age",24);
    //jsonObj.put("hobby",new String[]{"上网","游戏","跑步","音乐"});
    //System.out.println(jsonObj.toString());
    /**
     * json对象转换成java对象
     */
    Student stud = (Student) JSONObject.toBean(jsonObj,Student.class);
    System.out.println(stud.getName());
  }
}

PS:关于json操作,这里再为大家推荐几款比较实用的json在线工具供大家参考使用:

在线JSON代码检验、检验、美化、格式化工具:
http://tools.jb51.net/code/json

JSON在线格式化工具:
http://tools.jb51.net/code/jsonformat

在线XML/JSON互相转换工具:
http://tools.jb51.net/code/xmljson

json代码在线格式化/美化/压缩/编辑/转换工具:
http://tools.jb51.net/code/jsoncodeformat

在线json压缩/转义工具:
http://tools.jb51.net/code/json_yasuo_trans

更多关于java相关内容感兴趣的读者可查看本站专题:《Java操作json格式数据技巧总结》、《Java数组操作技巧总结》、《Java字符与字符串操作技巧总结》、《Java数学运算技巧总结》、《Java数据结构与算法教程》及《Java操作DOM节点技巧总结

希望本文所述对大家java程序设计有所帮助。

相关文章

  • SpringSecurity添加图形验证码认证实现

    SpringSecurity添加图形验证码认证实现

    本文主要介绍了SpringSecurity添加图形验证码认证实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08
  • 详解java nio中的select和channel

    详解java nio中的select和channel

    这篇文章主要介绍了java nio中的select和channel
    2019-05-05
  • SpringBoot登录用户权限拦截器

    SpringBoot登录用户权限拦截器

    这篇文章主要介绍了SpringBoot登录用户权限拦截器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • 详解Java中Math.round()的取整规则

    详解Java中Math.round()的取整规则

    这篇文章主要介绍了详解Java中Math.round()的取整规则,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • IDEA 报Plugin'maven-resources-plugin:'not found 的解决方案

    IDEA 报Plugin'maven-resources-plugin:'not found 

    如果在使用 IDEA 时遇到 "Plugin 'maven-resources-plugin:' not found" 错误,可能是由于 Maven 仓库中未找到所需的 Maven 插件,近小编给大家分享几种解决方法,感兴趣的朋友跟随小编一起看看吧
    2023-07-07
  • 解决SpringBoot搭建MyBatisPlus中selectList遇到LambdaQueryWrapper报错问题

    解决SpringBoot搭建MyBatisPlus中selectList遇到LambdaQueryWrapper报错问题

    这篇文章主要介绍了解决SpringBoot搭建MyBatisPlus中selectList遇到LambdaQueryWrapper报错问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • Java初学者了解

    Java初学者了解"=="与equals的区别

    这篇文章主要介绍了Java初学者了解"=="与equals的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11
  • Sentinel核心类之Entry类解读

    Sentinel核心类之Entry类解读

    这篇文章主要介绍了Sentinel核心类之Entry类解读,Sentinel中Entry可以理解为每次进入资源的一个凭证,如果调用SphO.entry()或者SphU.entry()能获取Entry对象,代表获取了凭证,没有被限流,否则抛出一个BlockException,需要的朋友可以参考下
    2023-12-12
  • java生成sm2/hutool生成公钥私钥代码示例

    java生成sm2/hutool生成公钥私钥代码示例

    这篇文章主要给大家介绍了关于java生成sm2/hutool生成公钥私钥的相关资料,Java是一种广泛使用的编程语言,可以用来生成公钥和私钥文件,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2023-09-09
  • 利用POI读取word、Excel文件的最佳实践教程

    利用POI读取word、Excel文件的最佳实践教程

    Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能。 下面这篇文章主要给大家介绍了关于利用POI读取word、Excel文件的最佳实践的相关资料,需要的朋友可以参考下。
    2017-11-11

最新评论