读书人

json-lib的一些运用

发布时间: 2012-10-09 10:21:45 作者: rapoo

json-lib的一些应用

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 */@Componentpublic 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);@ResourceJsonTest 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";}/** * 调用返回JSON格式的字符串的方法,如要返回到页面, 需要将字符串重新转化为JSONObject对象 *  * @param map * @return */@RequestMapping(value = "array2ToJson")public String array2ToJson(ModelMap map) {String jsonStr = jsonTest.array2ToJson();log.info("数据:" + jsonStr);map.put("json", JSONObject.fromObject(jsonStr));return "json/array2ToJson";}/** * 调用直接返回JSONObject的方法,直接put到页面即可 *  * @param map * @return */@RequestMapping(value = "array2ToJson2")public String array2ToJson2(ModelMap map) {map.put("json", jsonTest.array2ToJson2());return "json/array2ToJson";}/** * 返回单个JSONObject对象,本方法直接调用了一个返回单个JSONObject对象的方法, * 该JSONObject对象中只放了一个实体bean方法 *  * @param map * @return */@RequestMapping(value = "objectToJson")public String objectToJson(ModelMap map) {JSONObject jsonObject = jsonTest.objectToJson();log.info("对象:" + jsonObject);map.put("json", jsonObject);return "json/objectToJson";}}
?
package com.yt.manager.json;/** * @Description: * @ClassName: Student * @Project: base-info * @Author: zxf * @Date: 2011-6-14 */public class Student {public String id;public int age;public String name;public String sex;public String getId() {return id;}public void setId(String id) {this.id = id;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}}
?

读书人网 >JavaScript

热点推荐