栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java对象和json的来回转换知识点总结

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java对象和json的来回转换知识点总结

为了是java中的对象便于理解,我们可以使用一款比较好用的数据格式,在数据解析的时候也会经常用到,它就是JSON。在这里我们转换对象和字符串时,需要java先变成json对象的模式。为了防止有人对JSON数组和对象的概念混淆,我们会先对这两个概念理解,然后带来java对象和json的来回转换的方法。

1.JSON数组和对象的区别

JSONArray是将数据转换为数组形式:

strArray:[{“address”:”北京市西城区”,”age”:”23”,”name”:”JSON”}]

使用时需要用数组方式读取json里面的数据,strArray[0].address;

JSONObject是将数据转换为对象形式:

strJson:{“address”:”北京市西城区”,”age”:”23”,”name”:”JSON”}

使用时直接使用对象方式读取json里面的数据,strArray.address;

2.对象转换为JSON

先将java对象转换为json对象,在将json对象转换为json字符串

//1、使用JSonObject
JSonObject json = JSONObject.fromObject(stu);
//2、使用JSonArray
JSonArray array=JSONArray.fromObject(stu);  
String strJson=json.toString();
String strArray=array.toString();

3.json字符串转换为java对象

同样先将json字符串转换为json对象,再将json对象转换为java对象,如下所示。

JSonObject obj = new JSonObject().fromObject(jsonStr);//将json字符串转换为json对象

将json对象转换为java对象

Person jb = (Person)JSONObject.toBean(obj,Person.class);//将建json对象转换为Person对象

实例扩展:

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 list = new ArrayList();
  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() {
  
  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"));
 }
 
 private static void TestJsonBean() {
  
  Student student = new Student();
  student.setId(1);
  student.setName("jag");
  student.setSex("man");
  student.setAge(25);
  student.setHobby(new String[]{"篮球","上网","跑步","游戏"});
  
  JSonObject jsonStu = JSONObject.fromObject(student);
  System.out.println(jsonStu.toString());
  System.out.println(jsonStu.getJSonArray("hobby"));
  
  Student stu = (Student) JSONObject.toBean(jsonStu, Student.class);
  System.out.println(stu.getName());
  
  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());
  
  Student stud = (Student) JSONObject.toBean(jsonObj,Student.class);
  System.out.println(stud.getName());
 }
}

到此这篇关于java对象和json的来回转换知识点总结的文章就介绍到这了,更多相关java对象和json的来回转换内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/129367.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号