package com.yt.manager.json;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Component;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* @Description:将对象转化为json格式
* @ClassName: JsonTest
* @Project: base-info
* @Author: zxf
* @Date: 2011-6-14
*/
@Component
public class JsonTest {
/**
* 把单个实体Bean转化为JSON格式
* @return JSONObject
*/
public JSONObject objectToJson() {
Student student = new Student();
student.setAge(18);
student.setName("zhangsan");
student.setSex("male");
return JSONObject.fromObject(student);
}
/**
* 把实体Bean数组转化成JSONArray对象
* @return JSON格式的字符串
*/
public String arrayToJson() {
Student stu = null;
List<Student> stuList = new ArrayList<Student>();
for (int i = 0; i < 5; i++) {
stu = new Student();
stu.setId(i + 1 + "");
stu.setAge(i * 10 + 8);
stu.setName("张三" + i);
stu.setSex("和");
stuList.add(stu);
}
JSONArray jsonArray = JSONArray.fromObject(stuList);
return jsonArray.toString();
}
/**
* 把实体Bean数组转化成JSONArray对象
* @return JSONArray
*/
public JSONArray arrayToJson2() {
Student stu = null;
List<Student> stuList = new ArrayList<Student>();
for (int i = 0; i < 5; i++) {
stu = new Student();
stu.setId(i + 1 + "");
stu.setAge(i * 10 + 8);
stu.setName("张三" + i);
stu.setSex("和");
stuList.add(stu);
}
return JSONArray.fromObject(stuList);
}
/**
* 多维JSON格式数组
*
* @return JSON格式的字符串
*/
public String array2ToJson() {
Student stu = null;
List<Student> stuList = new ArrayList<Student>();
for (int i = 0; i < 5; i++) {
stu = new Student();
stu.setId(i + 1 + "");
stu.setAge(i * 10 + 8);
stu.setName("张三" + i);
stu.setSex("和");
stuList.add(stu);
}
Map<String, Object> map = new HashMap<String, Object>();
map.put("list", stuList);
map.put("count", "10");
return JSONObject.fromObject(map).toString();
}
/**
* 多维JSON格式数组
*
* @return JSONObject
*/
public JSONObject array2ToJson2() {
Student stu = null;
List<Student> stuList = new ArrayList<Student>();
for (int i = 0; i < 5; i++) {
stu = new Student();
stu.setId(i + 1 + "");
stu.setAge(i * 10 + 8);
stu.setName("张三" + i);
stu.setSex("和");
stuList.add(stu);
}
Map<String, Object> map = new HashMap<String, Object>();
map.put("list", stuList);
map.put("count", "10");
return JSONObject.fromObject(map);
}
/**
* @param args
*/
public static void main(String[] args) {
JsonTest t = new JsonTest();
t.arrayToJson();
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Component;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* @Description:将对象转化为json格式
* @ClassName: JsonTest
* @Project: base-info
* @Author: zxf
* @Date: 2011-6-14
*/
@Component
public class JsonTest {
/**
* 把单个实体Bean转化为JSON格式
* @return JSONObject
*/
public JSONObject objectToJson() {
Student student = new Student();
student.setAge(18);
student.setName("zhangsan");
student.setSex("male");
return JSONObject.fromObject(student);
}
/**
* 把实体Bean数组转化成JSONArray对象
* @return JSON格式的字符串
*/
public String arrayToJson() {
Student stu = null;
List<Student> stuList = new ArrayList<Student>();
for (int i = 0; i < 5; i++) {
stu = new Student();
stu.setId(i + 1 + "");
stu.setAge(i * 10 + 8);
stu.setName("张三" + i);
stu.setSex("和");
stuList.add(stu);
}
JSONArray jsonArray = JSONArray.fromObject(stuList);
return jsonArray.toString();
}
/**
* 把实体Bean数组转化成JSONArray对象
* @return JSONArray
*/
public JSONArray arrayToJson2() {
Student stu = null;
List<Student> stuList = new ArrayList<Student>();
for (int i = 0; i < 5; i++) {
stu = new Student();
stu.setId(i + 1 + "");
stu.setAge(i * 10 + 8);
stu.setName("张三" + i);
stu.setSex("和");
stuList.add(stu);
}
return JSONArray.fromObject(stuList);
}
/**
* 多维JSON格式数组
*
* @return JSON格式的字符串
*/
public String array2ToJson() {
Student stu = null;
List<Student> stuList = new ArrayList<Student>();
for (int i = 0; i < 5; i++) {
stu = new Student();
stu.setId(i + 1 + "");
stu.setAge(i * 10 + 8);
stu.setName("张三" + i);
stu.setSex("和");
stuList.add(stu);
}
Map<String, Object> map = new HashMap<String, Object>();
map.put("list", stuList);
map.put("count", "10");
return JSONObject.fromObject(map).toString();
}
/**
* 多维JSON格式数组
*
* @return JSONObject
*/
public JSONObject array2ToJson2() {
Student stu = null;
List<Student> stuList = new ArrayList<Student>();
for (int i = 0; i < 5; i++) {
stu = new Student();
stu.setId(i + 1 + "");
stu.setAge(i * 10 + 8);
stu.setName("张三" + i);
stu.setSex("和");
stuList.add(stu);
}
Map<String, Object> map = new HashMap<String, Object>();
map.put("list", stuList);
map.put("count", "10");
return JSONObject.fromObject(map);
}
/**
* @param args
*/
public static void main(String[] args) {
JsonTest t = new JsonTest();
t.arrayToJson();
}
}