@Pollux
2016-07-23T03:22:07.000000Z
字数 3519
阅读 782
web
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.7.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.7.5</version>
</dependency>
package com.xie.pojo;
public class user {
private String xie;
private String yxin;
private String godlikexie;
public String getXie() {
return xie;
}
public void setXie(String xie) {
this.xie = xie;
}
public String getYxin() {
return yxin;
}
public void setYxin(String yxin) {
this.yxin = yxin;
}
public String getGodlikexie() {
return godlikexie;
}
public void setGodlikexie(String godlikexie) {
this.godlikexie = godlikexie;
}
@Override
public String toString() {
return "user [xie=" + xie + ", yxin=" + yxin + ", godlikexie=" + godlikexie + "]";
}
}
package com.pollux.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
@Controller
public class TestJson {
@ResponseBody
@RequestMapping(value = "testjsonbean", method = RequestMethod.GET)
public user testjsonbean() {
user user = new user();
user.setXie("godlikexie");
user.setYxin("yxin");
return user;
}
@ResponseBody
@RequestMapping(value = "testjsonlist" , method = RequestMethod.GET)
public List<String> testjsonlist(){
List<String> xie = new ArrayList<String>();
xie.add("godlikexie");
xie.add("xie");
xie.add("yxin");
return xie;
}
@ResponseBody
@RequestMapping(value = "testjsonmap" , method = RequestMethod.GET)
public Map<String,String> testjsonmap(){
Map<String,String> xie = new HashMap<String,String>();
xie.put("xie", "isgod");
xie.put("yxin", "ismywife");
xie.put("godlikexie", "invincible");
return xie;
}
@ResponseBody
@RequestMapping(value = "testdojson", method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
public String testdojson() throws JsonParseException, JsonMappingException, IOException {
String json = "{\"xie\":\"isgod\",\"yxin\": \"godness\",\"godlikexie\": \"invincible\"}";
ObjectMapper objectMapper = new ObjectMapper();
String jsonresult = null;
user user = objectMapper.readValue(json, user.class);
if (user != null) {
jsonresult = user.toString();
System.out.println(jsonresult);
}
return jsonresult;
}
}
//输出结果
{"username":"godlikexie","password":"yxin"}
["godlikexie","xie","yxin"]
{"xie":"god","yxin":"godness","godlikexie":"invincible"}
@ResponseBody
@RequestMapping(value = "testdojson", method = RequestMethod.GET)
public String testdojson() throws JsonParseException, JsonMappingException, IOException {
String json = "{\"xie\":\"isgod\",\"yxin\": \"godness\",\"godlikexie\": \"invincible\"}";
ObjectMapper objectMapper = new ObjectMapper();
String jsonresult = null;
user user = objectMapper.readValue(json, user.class);
if (user != null) {
jsonresult = user.toString();
System.out.println(jsonresult);
}
return jsonresult;
}
//输出结果
user [xie=isgod, yxin=godness, godlikexie=invincible]