日期:2014-05-16 浏览次数:20342 次
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(); } }?
package com.yt.manager.json.controller; import javax.annotation.Resource; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import com.yt.manager.json.JsonTest; /** * @Description:获取json格式的内容request到页面 * @ClassName: JsonController * @Project: base-info * @Author: zxf * @Date: 2011-6-15 */ @Controller @RequestMapping("/json") public class JsonController { Logger log = Logger.getLogger(JsonController.class); @Resource JsonTest jsonTest; /** * 调用了一个返回Json数组格式的字符串,需要将字符串转化为JSONArray格式的数组,才能put到页面 * @param map * @return */ @RequestMapping(value = "arrayToJson") public String arrayToJson(ModelMap map) { String jsonStr = jsonTest.arrayToJson(); log.info("数据:" + jsonStr); map.put("json", JSONArray.fromObject(jsonStr)); return "json/arrayToJson"; } /** * 调用了一个返回JSONArray对象的数组,不需要任何转化即可put到页面 * @param map * @return */ @RequestMapping(value = "arrayToJson2") public String arrayToJson2(ModelMap map) { map.put("json", jsonTest.arrayToJson2()); return "json/arrayToJson"; }