FastJson json和Objec互相轉換

package com.mtsoft.wx.menu;

import java.util.ArrayList;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mtsoft.wx.msg.Student;
import com.mtsoft.wx.msg.UserDemo;
import com.mtsoft.wx.msg.UserInfo;

public class JsonTest {
	public JsonTest() {
		Student stu1 = new Student();
		stu1.setId(1);
		stu1.setUserName("nick");
		stu1.setAge(34);
		Student stu2 = new Student();
		stu2.setId(2);
		stu2.setUserName("ning");
		stu2.setAge(25);
		ArrayList<Student> stuList = new ArrayList<Student>();
		stuList.add(stu1);
		stuList.add(stu2);
		
		UserInfo userInfo = new UserInfo();
		userInfo.setUserId(001);
		userInfo.setStudentList(stuList);
		
		UserDemo userDemo = new UserDemo();
		userDemo.setUserInfo(userInfo);
		
		//object轉json
		JSONObject jsonObject = (JSONObject) JSONObject.toJSON(userDemo);
		String json = jsonObject.toString();
		System.out.println(json);
		
		//json轉Object
		UserDemo userDemo1 =  JSON.parseObject(json, UserD
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章