將java對象轉換爲Json

package com.ysstech.test.json;

import java.util.ArrayList;
import java.util.List;

import com.ysstech.test.createExcel.po.Student;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Json_Object2Json {
	public static void main(String[] args) {
		Student student = new Student();
		student.setName("ssk");
		student.setAge(16);
		student.setGender("男");
		//將對象轉化爲json
		JSONObject jsonObject = new JSONObject().fromObject(student);
		System.out.println(jsonObject);
		
		
		Student student2 = new Student();
		student2.setName("ggg");
		student2.setAge(16);
		student2.setGender("女");
		
		List<Student> list = new ArrayList<Student>();
		list.add(student);
		list.add(student2);
		//將集合轉化爲數組
		JSONArray jsonArray =JSONArray.fromObject(list);
		
		System.out.println(jsonArray);
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章