日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java对象和json对象之间互相转换

發布時間:2023/12/19 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java对象和json对象之间互相转换 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

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());
?? ??? ?System.out.println("-----------------------");

?? ??? ?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());
?? ??? ??? ?}
?? ??? ??? ?System.out.println("-----------------------");
?? ??? ?}
?? ?}

?? ?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"));
?? ??? ?System.out.println("-----------------------");
?? ?}

?? ?/**
?? ? * 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"));
?? ??? ?System.out.println("-----------------------");
?? ??? ?/**
?? ??? ? * json對象轉換成java對象,并獲取java對象屬性
?? ??? ? */
?? ??? ?Student stu = (Student) JSONObject.toBean(jsonStu, Student.class);
?? ??? ?System.out.println(stu.getName());
?? ??? ?System.out.println("-----------------------");
?? ??? ?/**
?? ??? ? * 創建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());
?? ??? ?System.out.println("-----------------------");
?? ?}
}

轉載于:https://my.oschina.net/u/1161889/blog/412090

總結

以上是生活随笔為你收集整理的java对象和json对象之间互相转换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。