[关闭]
@Pollux 2016-07-23T03:22:07.000000Z 字数 3519 阅读 782

spring-mvc json

web


  1. <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
  2. <dependency>
  3. <groupId>com.fasterxml.jackson.core</groupId>
  4. <artifactId>jackson-core</artifactId>
  5. <version>2.7.5</version>
  6. </dependency>
  7. <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
  8. <dependency>
  9. <groupId>com.fasterxml.jackson.core</groupId>
  10. <artifactId>jackson-databind</artifactId>
  11. <version>2.7.5</version>
  12. </dependency>
  13. <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
  14. <dependency>
  15. <groupId>com.fasterxml.jackson.core</groupId>
  16. <artifactId>jackson-annotations</artifactId>
  17. <version>2.7.5</version>
  18. </dependency>
  1. package com.xie.pojo;
  2. public class user {
  3. private String xie;
  4. private String yxin;
  5. private String godlikexie;
  6. public String getXie() {
  7. return xie;
  8. }
  9. public void setXie(String xie) {
  10. this.xie = xie;
  11. }
  12. public String getYxin() {
  13. return yxin;
  14. }
  15. public void setYxin(String yxin) {
  16. this.yxin = yxin;
  17. }
  18. public String getGodlikexie() {
  19. return godlikexie;
  20. }
  21. public void setGodlikexie(String godlikexie) {
  22. this.godlikexie = godlikexie;
  23. }
  24. @Override
  25. public String toString() {
  26. return "user [xie=" + xie + ", yxin=" + yxin + ", godlikexie=" + godlikexie + "]";
  27. }
  28. }
  1. package com.pollux.controller;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestMethod;
  10. import org.springframework.web.bind.annotation.ResponseBody;
  11. import com.fasterxml.jackson.core.JsonParseException;
  12. import com.fasterxml.jackson.databind.JsonMappingException;
  13. import com.fasterxml.jackson.databind.ObjectMapper;
  14. @Controller
  15. public class TestJson {
  16. @ResponseBody
  17. @RequestMapping(value = "testjsonbean", method = RequestMethod.GET)
  18. public user testjsonbean() {
  19. user user = new user();
  20. user.setXie("godlikexie");
  21. user.setYxin("yxin");
  22. return user;
  23. }
  24. @ResponseBody
  25. @RequestMapping(value = "testjsonlist" , method = RequestMethod.GET)
  26. public List<String> testjsonlist(){
  27. List<String> xie = new ArrayList<String>();
  28. xie.add("godlikexie");
  29. xie.add("xie");
  30. xie.add("yxin");
  31. return xie;
  32. }
  33. @ResponseBody
  34. @RequestMapping(value = "testjsonmap" , method = RequestMethod.GET)
  35. public Map<String,String> testjsonmap(){
  36. Map<String,String> xie = new HashMap<String,String>();
  37. xie.put("xie", "isgod");
  38. xie.put("yxin", "ismywife");
  39. xie.put("godlikexie", "invincible");
  40. return xie;
  41. }
  42. @ResponseBody
  43. @RequestMapping(value = "testdojson", method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
  44. public String testdojson() throws JsonParseException, JsonMappingException, IOException {
  45. String json = "{\"xie\":\"isgod\",\"yxin\": \"godness\",\"godlikexie\": \"invincible\"}";
  46. ObjectMapper objectMapper = new ObjectMapper();
  47. String jsonresult = null;
  48. user user = objectMapper.readValue(json, user.class);
  49. if (user != null) {
  50. jsonresult = user.toString();
  51. System.out.println(jsonresult);
  52. }
  53. return jsonresult;
  54. }
  55. }
  1. //输出结果
  2. {"username":"godlikexie","password":"yxin"}
  3. ["godlikexie","xie","yxin"]
  4. {"xie":"god","yxin":"godness","godlikexie":"invincible"}
  1. @ResponseBody
  2. @RequestMapping(value = "testdojson", method = RequestMethod.GET)
  3. public String testdojson() throws JsonParseException, JsonMappingException, IOException {
  4. String json = "{\"xie\":\"isgod\",\"yxin\": \"godness\",\"godlikexie\": \"invincible\"}";
  5. ObjectMapper objectMapper = new ObjectMapper();
  6. String jsonresult = null;
  7. user user = objectMapper.readValue(json, user.class);
  8. if (user != null) {
  9. jsonresult = user.toString();
  10. System.out.println(jsonresult);
  11. }
  12. return jsonresult;
  13. }
  1. //输出结果
  2. user [xie=isgod, yxin=godness, godlikexie=invincible]
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注