使用jackson的writeValueAsString把java对象输出成字符串实例,设置应答体的类型
来源:互联网 发布:淘宝如何发布商品 编辑:程序博客网 时间:2024/06/05 15:36
转载地址:http://www.360sdn.com/json/2013/0525/216.html
+http://hw1287789687.iteye.com/blog/2188480
要使用jackson的json解析器需要下载jackson的核心jar包:
jackson-core-2.2.0.jar jackson-core下载地址
jackson-annotations-2.2.0.jar jackson-annotations下载地址
jackson-databind-2.2.0.jar jackson-databind下载地址
一、首先定义一个java的pojo类,代码如下:
package
com;
public
class
BaseObject {
private
String userName;
private
String userCode;
private
double
weight;
private
int
height;
private
boolean
sex;
private
String[] array;
private
BaseObject innerObject;
public
String getUserName() {
return
userName;
}
public
void
setUserName(String userName) {
this
.userName = userName;
}
public
String getUserCode() {
return
userCode;
}
public
void
setUserCode(String userCode) {
this
.userCode = userCode;
}
public
double
getWeight() {
return
weight;
}
public
void
setWeight(
double
weight) {
this
.weight = weight;
}
public
int
getHeight() {
return
height;
}
public
void
setHeight(
int
height) {
this
.height = height;
}
public
boolean
isSex() {
return
sex;
}
public
void
setSex(
boolean
sex) {
this
.sex = sex;
}
public
BaseObject getInnerObject() {
return
innerObject;
}
public
void
setInnerObject(BaseObject innerObject) {
this
.innerObject = innerObject;
}
public
String[] getArray() {
return
array;
}
public
void
setArray(String[] array) {
this
.array = array;
}
}
二、把java的pojo类输出成json字符串
使用jackson的ObjectMapper 的writeValueAsString方法可以把pojo类输出成json字符串,代码如下:package
com;
import
java.util.ArrayList;
import
java.util.HashMap;
import
java.util.List;
import
java.util.Map;
import
com.fasterxml.jackson.databind.ObjectMapper;
import
com.BaseObject;
public
class
JacksonJsonTest {
public
static
void
main(String[] args){
JacksonJsonTest jsonImple=
new
JacksonJsonTest();
try
{
ObjectMapper mapper =
new
ObjectMapper();
List dataList=
new
ArrayList();
Map<string,object> map =
new
HashMap<string, object=
""
>();
BaseObject object1=
new
BaseObject();
object1.setUserName(
"张三"
);
object1.setWeight(
65.5
);
object1.setHeight(
170
);
object1.setSex(
true
);
String[] score={
"80"
,
"90"
,
"95"
};
object1.setArray(score);
BaseObject object2=
new
BaseObject();
object2.setUserName(
"李四"
);
object2.setWeight(
75.5
);
object2.setHeight(
171
);
object2.setSex(
true
);
score=
new
String[
3
];
score[
0
]=
"65"
;
score[
1
]=
"68"
;
score[
2
]=
"75"
;
object2.setArray(score);
object1.setInnerObject(object2);
dataList.add(object1);
map.put(
"baseObject"
, dataList);
String json=mapper.writeValueAsString(object1);
System.out.println(json);
}
catch
(Exception ex){
}
}
}
</string,></string,object>
输出的结果如下:
{"userName":"王五","userCode":null,"weight":65.5,
"height":170,"sex":true,"array":["语文:85",
"数学:80","英语:95"],"innerObject":{"userName":"赵六",
"userCode":null,"weight":75.5,"height":171,"sex":true,
"array":["语文:65","数学:68","英语:75"],"innerObject":null}}
spring MVC中如何设置应答体的content type呢?
spring MVC中如何设置返回类型呢?
我们知道response 的content type主要有:
text/html
text/plain
application/json;charset=UTF-8
application/octet-stream
等
先举一个例子,spring mvc中可以通过如下方式返回json字符串:
- @ResponseBody
- @RequestMapping(value = "/upload")
- public String upload(HttpServletRequest request, HttpServletResponse response,String contentType2)
- throws IOException {
- String content = null;
- Map map = new HashMap();
- ObjectMapper mapper = new ObjectMapper();
- map.put("fileName", "a.txt");
- try {
- content = mapper.writeValueAsString(map);
- System.out.println(content);
- } catch (JsonGenerationException e) {
- e.printStackTrace();
- } catch (JsonMappingException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return content;
- }
虽然访问时返回的确实是json字符串,但是response 的content type是"
text/html
"这不是我们期望的,我们期望的response content type是"application/json"或者"application/json;charset=UTF-8",那么如何实现呢?
通过注解@RequestMapping 中的produces
用法如下:
- @RequestMapping(value = "/upload",produces="application/json;charset=UTF-8")
spring MVC官方文档:
Producible Media Types
You can narrow the primary mapping by specifying a list of producible media types. The request will be matched only if the Accept request header matches one of these values. Furthermore, use of the produces condition ensures the actual content type used to generate the response respects the media types specified in the producescondition. For example:
@Controller@RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, produces="application/json")@ResponseBodypublic Pet getPet(@PathVariable String petId, Model model) { // implementation omitted}
Just like with consumes, producible media type expressions can be negated as in !text/plain to match to all requests other than those with an Accept header value oftext/plain.
TipThe produces condition is supported on the type and on the method level. Unlike most other conditions, when used at the type level, method-level producible types override rather than extend type-level producible types.
- 使用jackson的writeValueAsString把java对象输出成字符串实例,设置应答体的类型
- Android进步之十四:使用jackon的ObjectMapper的writeValueAsString方法可以把java对象转化成json字符串
- 如何利用jackson,把对象中值为null或“”(空字符串)的属性不输出出来
- 使用 Jackson 把 Java 对象转换成 JSON 字串
- 使用 Jackson 把 Java 对象转换成 JSON 字串
- jackson序列化java对象的使用
- 使用jackson对Java对象与JSON字符串相互转换的一些总结
- 使用jackson对Java对象与JSON字符串相互转换的一些总结
- 使用jackson对Java对象与JSON字符串相互转换的一些总结
- jackson json字符串、map、java对象的转换例子
- java jackson的使用
- 把对象转换成JSON格式的Object类型和String类型的字符串
- 把数值类型转换成货币字符串的形式输出的细节若干
- java-使用字符串输出对象
- 使用jackson对Java对象与JSON字符串相互转换
- 通过Jackson实现Java对象和json字符串的相互转换
- 用Jackson将Java对象转换成 JSON字符串
- 把NSString 类型的字符串转化成 utf-8 字符串
- 程序员面试金典——解题总结: 9.17中等难题 17.13将二叉查找树转换为双向链表
- 虚析构函数的作用
- 慎用数字的位移运算
- System UI 调试方法
- 数字组合 求最大长度
- 使用jackson的writeValueAsString把java对象输出成字符串实例,设置应答体的类型
- Android Studio 简单介绍和使用问题小结
- 国内值得关注的官方API集合
- hashMap实现原理
- 8天学通MongoDB——第八天 驱动实践
- 记一次对Gradle同一项目变种版本的配置
- 局域网访问VMware下的ubuntu中的http服务器
- RUNOOB.COM---SQLITE学习网站
- 基本数据类型,声明符,类型修饰符,变量标识符