用的springmvc框架结构,转换需要哪些架包。
导入Google的包gson-2.2.4.jar
然后实例化Gson
static Gson gosn = new Gson();
String json = gosn.toJson(hashMap); //这里放一个对象,什么对象都可以。
转化后就是Json,功能强大很多,也简单很多。
json-lib-2.4-jdk15.jar
ezmorph-1.0.6.jar
转换的话这样用
String s= JSONArray.fromObject(user).toString();
spring-webmvc4
在方法上加入@ResponseBody,同时方法返回值为实体对象,spring会自动将对象转换为json格式,并返回到客户端
然后实例化Gson
static Gson gosn = new Gson();
String json = gosn.toJson(hashMap); //这里放一个对象,什么对象都可以。
转化后就是Json,功能强大很多,也简单很多。
json-lib-2.4-jdk15.jar
ezmorph-1.0.6.jar
转换的话这样用
String s= JSONArray.fromObject(user).toString();
spring-webmvc4
在方法上加入@ResponseBody,同时方法返回值为实体对象,spring会自动将对象转换为json格式,并返回到客户端
温馨提示:答案为网友推荐,仅供参考
第1个回答 2016-01-09
实例代码:
public class JSONUtil {
/**
* 将json转化为实体POJO
* @param jsonStr
* @param obj
* @return
*/
public static<T> Object JSONToObj(String jsonStr,Class<T> obj) {
T t = null;
try {
ObjectMapper objectMapper = new ObjectMapper();
t = objectMapper.readValue(jsonStr,
obj);
} catch (Exception e) {
e.printStackTrace();
}
return t;
}
/**
* 将实体POJO转化为JSON
* @param obj
* @return
* @throws JSONException
* @throws IOException
*/
public static<T> JSONObject objectToJson(T obj) throws JSONException, IOException {
ObjectMapper mapper = new ObjectMapper();
// Convert object to JSON string
String jsonStr = "";
try {
jsonStr = mapper.writeValueAsString(obj);
} catch (IOException e) {
throw e;
}
return new JSONObject(jsonStr);
}
public static void main(String[] args) throws JSONException, IOException {
JSONObject obj = null;
obj = new JSONObject();
obj.put("name", "213");
obj.put("age", 27);
JSONArray array = new JSONArray();
array.put(obj);
obj = new JSONObject();
obj.put("name", "214");
obj.put("age", 28);
array.put(obj);
Student stu = (Student) JSONToObj(obj.toString(), Student.class);
JSONObject objList = new JSONObject();
objList.put("student", array);
System.out.println("objList:"+objList);
StudentList stuList = (StudentList) JSONToObj(objList.toString(), StudentList.class);
System.out.println("student:"+stu);
System.out.println("stuList:"+stuList);
System.out.println("#####################################");
JSONObject getObj = objectToJson(stu);
System.out.println(getObj);
}
}
public class JSONUtil {
/**
* 将json转化为实体POJO
* @param jsonStr
* @param obj
* @return
*/
public static<T> Object JSONToObj(String jsonStr,Class<T> obj) {
T t = null;
try {
ObjectMapper objectMapper = new ObjectMapper();
t = objectMapper.readValue(jsonStr,
obj);
} catch (Exception e) {
e.printStackTrace();
}
return t;
}
/**
* 将实体POJO转化为JSON
* @param obj
* @return
* @throws JSONException
* @throws IOException
*/
public static<T> JSONObject objectToJson(T obj) throws JSONException, IOException {
ObjectMapper mapper = new ObjectMapper();
// Convert object to JSON string
String jsonStr = "";
try {
jsonStr = mapper.writeValueAsString(obj);
} catch (IOException e) {
throw e;
}
return new JSONObject(jsonStr);
}
public static void main(String[] args) throws JSONException, IOException {
JSONObject obj = null;
obj = new JSONObject();
obj.put("name", "213");
obj.put("age", 27);
JSONArray array = new JSONArray();
array.put(obj);
obj = new JSONObject();
obj.put("name", "214");
obj.put("age", 28);
array.put(obj);
Student stu = (Student) JSONToObj(obj.toString(), Student.class);
JSONObject objList = new JSONObject();
objList.put("student", array);
System.out.println("objList:"+objList);
StudentList stuList = (StudentList) JSONToObj(objList.toString(), StudentList.class);
System.out.println("student:"+stu);
System.out.println("stuList:"+stuList);
System.out.println("#####################################");
JSONObject getObj = objectToJson(stu);
System.out.println(getObj);
}
}