软件定制开发供应商Java中JSON字符串和Java对象的互转

1.JSON数据和Java软件定制开发供应商软件定制开发供应商对象的相互转换

 JSON数据和Java对象的相互转换

    * JSON解析器:
        * 软件定制开发供应商常见的解析器:Jsonlib,Gson,,jackson
    1. Java对象转换JSON
        1. 使用步骤:
            1. 导入jackson的相关jar包
            2. 创建Jackson核心对象 ObjectMapper
            3. 调用ObjectMapper软件定制开发供应商的相关方法进行转换
                1. 转换方法:
                    * writeValue(参数1,obj):
                        参数1:
                            File:将obj软件定制开发供应商软件定制开发供应商对象转换为JSON字符串,软件定制开发供应商并保存到指定的文件中
                            Writer:将obj对象转换为JSON字符串,并将json数据填充到字符输出流中
                            OutputStream:将obj对象转换为JSON字符串,并将json数据填充到字节输出流中
                    * writeValueAsString(obj):将对象转为json字符串

                2. 注解:
                    1. @JsonIgnore:排除某个属性不要转换成JSON,给类的属性上加上这个注解。
                    2. @JsonFormat:属性值得格式化日期字符串,取的是默认时区的时间
                        * @JsonFormat(pattern = "yyyy-MM-dd")
                           private Date birthday;
                           
                         @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")//给默认时区加上8个小时
                    3.指定转换json字符串是的别名 @JsonProperty("username")     
                     @JsonProperty("username")
                     private String name;
                     
                    4. 如果该字段的值是null,就不会转换成JSON
                     @JsonInclude(JsonInclude.Include.NON_NULL) //如果该字段的值是null,就不会转换成JSON
                      private Long num; //null
                3. 复杂java对象转换
                    1. List:数组
                    2. Map:对象格式一致
                    
    2. JSON转为Java对象
        1. 导入jackson的相关jar包
        2. 创建Jackson核心对象 ObjectMapper
        3. 调用ObjectMapper的相关方法进行转换
            1. readValue(json字符串数据,Class)                

* Ajax的应用:校验用户名是否存在
    1. 服务器响应的数据,在客户端使用时,要想当做json数据格式使用。有两种解决方案:
        1. $.get(type):将最后一个参数type指定为"json"
        2. 在服务器端设置MIME类型
            response.setContentType("application/json;charset=utf-8");
            //设置跨域请求
            response.setHeader("Access-Control-Allow-Origin","*");


2.JSON字符串转换Java对象(使用截取字符串的方法)

测试类:

  1. public class MyTest {
  2. public static void main(String[] args) {
  3. //前台给后台提交的数据,常见的有两种格式
  4. // username=zhangsan&password=123456&age=23
  5. //{"username":"张三","password":"123456"}
  6. //把json字符串转换成java对象
  7. String jsonStr = "{\"username\":\"张三\",\"password\":\"123456\"}";
  8. String s = jsonStr.replaceAll("[{}\"]", "");
  9. System.out.println(s);
  10. String[] strings = s.split(",");
  11. System.out.println(strings[0]);
  12. System.out.println(strings[1]);
  13. String[] a = strings[0].split(":");
  14. System.out.println(a[0]);
  15. System.out.println(a[1]);
  16. String[] b = strings[1].split(":");
  17. System.out.println(b[0]);
  18. System.out.println(b[1]);
  19. User user = new User();
  20. user.setUsername(a[1]);
  21. user.setPasswrod(b[1]);
  22. System.out.println(user);
  23. }
  24. }

User对象:

  1. public class User {
  2. private String username;
  3. private String passwrod;
  4. public String getUsername() {
  5. return username;
  6. }
  7. public void setUsername(String username) {
  8. this.username = username;
  9. }
  10. public String getPasswrod() {
  11. return passwrod;
  12. }
  13. public void setPasswrod(String passwrod) {
  14. this.passwrod = passwrod;
  15. }
  16. @Override
  17. public String toString() {
  18. return "User{" +
  19. "username='" + username + '\'' +
  20. ", passwrod='" + passwrod + '\'' +
  21. '}';
  22. }
  23. }


3.java对象转成json字符串(保存到文件)

普通转换(String jsonStr = “{“username”:“张三”,“password”:“123456”}”;)

  1. import com.fasterxml.jackson.databind.ObjectMapper;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. public class MyTest {
  6. public static void main(String[] args) throws IOException {
  7. //Java对象转换成JSON字符串
  8. //String jsonStr = "{\"username\":\"张三\",\"password\":\"123456\"}";
  9. User user = new User("王五", "123456", 20, "18856259632");
  10. Car car = new Car();
  11. car.setCarName("宝马");
  12. car.setCarPrice(888888.0);
  13. //user.setCar(car);
  14. ArrayList<String> list = new ArrayList<>();
  15. list.add("张曼玉");
  16. list.add("王祖贤");
  17. user.setGirlfriend(list);
  18. ObjectMapper mapper = new ObjectMapper();
  19. String jsonStr = mapper.writeValueAsString(user);
  20. System.out.println(jsonStr);
  21. //{"username":"王五","password":"123456","age":20,"phoneNum":"18856259632",car:{carName:"宝马",carPrice:8888},girlfriend:["刘亦菲","张曼玉"]}
  22. //把转好的数据保存到文件中
  23. mapper.writeValue(new File("a.json"), user);
  24. }
  25. }

数组嵌套json( [{},{},{}] )

  1. import com.fasterxml.jackson.databind.ObjectMapper;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. public class MyTest2 {
  6. public static void main(String[] args) throws IOException {
  7. //[{},{},{}]
  8. User user1 = new User("王五", "12345685", 20, "18856259632");
  9. User user2 = new User("赵六", "12345685", 28, "18856259632");
  10. User user3 = new User("田七", "12345776", 24, "18856259632");
  11. ArrayList<User> list = new ArrayList<>();
  12. list.add(user1);
  13. list.add(user2);
  14. list.add(user3);
  15. ObjectMapper mapper = new ObjectMapper();
  16. String s = mapper.writeValueAsString(list);
  17. System.out.println(s);
  18. //把转好的数据保存到文件中
  19. mapper.writeValue(new File("b.json"), list);
  20. }
  21. }

{“user1”:{},“user2”:{},“user3”:{}}

  1. import com.fasterxml.jackson.databind.ObjectMapper;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.HashMap;
  5. public class MyTest3 {
  6. public static void main(String[] args) throws IOException {
  7. //{"user1":{},"user2":{},"user3":{}}
  8. User user1 = new User("王五", "12345685", 20, "18856259632");
  9. User user2 = new User("赵六", "12345685", 28, "18856259632");
  10. User user3 = new User("田七", "12345776", 24, "18856259632");
  11. HashMap<String, User> hm = new HashMap<>();
  12. hm.put("user1", user1);
  13. hm.put("user2", user2);
  14. hm.put("user3", user3);
  15. ObjectMapper mapper = new ObjectMapper();
  16. String s = mapper.writeValueAsString(hm);
  17. System.out.println(s);
  18. //把转好的数据保存到文件中
  19. mapper.writeValue(new File("c.json"), hm);
  20. }
  21. }

User类和Car类

  1. public class User {
  2. private String username;
  3. private String password;
  4. private int age;
  5. private String phoneNum;
  6. private Car car;
  7. private List<String> girlfriend;
  8. public User() {
  9. }
  10. public User(String username, String password, int age, String phoneNum) {
  11. this.username = username;
  12. this.password = password;
  13. this.age = age;
  14. this.phoneNum = phoneNum;
  15. }
  16. public User(String username, String password, int age, String phoneNum, Car car, List<String> girlfriend) {
  17. this.username = username;
  18. this.password = password;
  19. this.age = age;
  20. this.phoneNum = phoneNum;
  21. this.car = car;
  22. this.girlfriend = girlfriend;
  23. }
  24. public List<String> getGirlfriend() {
  25. return girlfriend;
  26. }
  27. public void setGirlfriend(List<String> girlfriend) {
  28. this.girlfriend = girlfriend;
  29. }
  30. public Car getCar() {
  31. return car;
  32. }
  33. public void setCar(Car car) {
  34. this.car = car;
  35. }
  36. public String getUsername() {
  37. return username;
  38. }
  39. public void setUsername(String username) {
  40. this.username = username;
  41. }
  42. public String getPassword() {
  43. return password;
  44. }
  45. public void setPassword(String password) {
  46. this.password = password;
  47. }
  48. public int getAge() {
  49. return age;
  50. }
  51. public void setAge(int age) {
  52. this.age = age;
  53. }
  54. public String getPhoneNum() {
  55. return phoneNum;
  56. }
  57. public void setPhoneNum(String phoneNum) {
  58. this.phoneNum = phoneNum;
  59. }
  60. }
  1. public class Car {
  2. private String carName;
  3. private Double carPrice;
  4. public String getCarName() {
  5. return carName;
  6. }
  7. public void setCarName(String carName) {
  8. this.carName = carName;
  9. }
  10. public Double getCarPrice() {
  11. return carPrice;
  12. }
  13. public void setCarPrice(Double carPrice) {
  14. this.carPrice = carPrice;
  15. }
  16. @Override
  17. public String toString() {
  18. return "Car{" +
  19. "carName='" + carName + '\'' +
  20. ", carPrice=" + carPrice +
  21. '}';
  22. }
  23. }


4.注解的作用

测试类:

  1. import com.fasterxml.jackson.core.JsonProcessingException;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import java.util.Date;
  4. public class MyTest {
  5. public static void main(String[] args) throws JsonProcessingException {
  6. User user = new User();
  7. user.setUsername("zhangsan");
  8. user.setAge(12);
  9. user.setPassword("999999");
  10. user.setPhoneNum("110");
  11. user.setBirthday(new Date());
  12. user.setSal(2000.0);
  13. ObjectMapper mapper = new ObjectMapper();
  14. String s = mapper.writeValueAsString(user);
  15. System.out.println(s);
  16. }
  17. }

User类:

  1. import com.fasterxml.jackson.annotation.JsonFormat;
  2. import com.fasterxml.jackson.annotation.JsonIgnore;
  3. import com.fasterxml.jackson.annotation.JsonInclude;
  4. import com.fasterxml.jackson.annotation.JsonProperty;
  5. import java.util.Date;
  6. public class User {
  7. @JsonProperty("name") //指定json字符中键的名称
  8. private String username;
  9. @JsonIgnore//排除某个属性不要转换成JSON,给类的属性上加上这个注解。
  10. private String password;
  11. private int age;
  12. //@JsonIgnore
  13. private String phoneNum;
  14. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
  15. private Date birthday;
  16. @JsonInclude(JsonInclude.Include.NON_NULL)
  17. private Double sal;
  18. public Double getSal() {
  19. return sal;
  20. }
  21. public void setSal(Double sal) {
  22. this.sal = sal;
  23. }
  24. public User() {
  25. }
  26. public User(String username, String password, int age, String phoneNum) {
  27. this.username = username;
  28. this.password = password;
  29. this.age = age;
  30. this.phoneNum = phoneNum;
  31. }
  32. public Date getBirthday() {
  33. return birthday;
  34. }
  35. public void setBirthday(Date birthday) {
  36. this.birthday = birthday;
  37. }
  38. public String getUsername() {
  39. return username;
  40. }
  41. public void setUsername(String username) {
  42. this.username = username;
  43. }
  44. public String getPassword() {
  45. return password;
  46. }
  47. public void setPassword(String password) {
  48. this.password = password;
  49. }
  50. public int getAge() {
  51. return age;
  52. }
  53. public void setAge(int age) {
  54. this.age = age;
  55. }
  56. public String getPhoneNum() {
  57. return phoneNum;
  58. }
  59. public void setPhoneNum(String phoneNum) {
  60. this.phoneNum = phoneNum;
  61. }
  62. }


5.JSON字符串转换成Java对象

测试类:

  1. import com.fasterxml.jackson.databind.ObjectMapper;
  2. import java.io.IOException;
  3. public class MyTest {
  4. public static void main(String[] args) throws IOException {
  5. String jsonStr = "{\"username\":\"张三\",\"password\":\"123456\"}";
  6. //保证你提供的Java类的属性名和类型以及层级结构和json字符串一一对应即可。
  7. ObjectMapper mapper = new ObjectMapper();
  8. User user = mapper.readValue(jsonStr, User.class);
  9. System.out.println(user);
  10. }
  11. }

User类:

  1. public class User {
  2. private String username;
  3. private String password;
  4. public String getUsername() {
  5. return username;
  6. }
  7. public void setUsername(String username) {
  8. this.username = username;
  9. }
  10. public String getPassword() {
  11. return password;
  12. }
  13. public void setPassword(String password) {
  14. this.password = password;
  15. }
  16. @Override
  17. public String toString() {
  18. return "User{" +
  19. "username='" + username + '\'' +
  20. ", password='" + password + '\'' +
  21. '}';
  22. }
  23. }


6.复杂的JSON字符串转换成Java对象

Car类:

  1. public class Car {
  2. private String carName;
  3. private Double carPrice;
  4. public String getCarName() {
  5. return carName;
  6. }
  7. public void setCarName(String carName) {
  8. this.carName = carName;
  9. }
  10. public Double getCarPrice() {
  11. return carPrice;
  12. }
  13. public void setCarPrice(Double carPrice) {
  14. this.carPrice = carPrice;
  15. }
  16. @Override
  17. public String toString() {
  18. return "Car{" +
  19. "carName='" + carName + '\'' +
  20. ", carPrice=" + carPrice +
  21. '}';
  22. }
  23. }

House类:

  1. public class House {
  2. private String houseName;
  3. private Double housePrice;
  4. public String getHouseName() {
  5. return houseName;
  6. }
  7. public void setHouseName(String houseName) {
  8. this.houseName = houseName;
  9. }
  10. public Double getHousePrice() {
  11. return housePrice;
  12. }
  13. public void setHousePrice(Double housePrice) {
  14. this.housePrice = housePrice;
  15. }
  16. @Override
  17. public String toString() {
  18. return "House{" +
  19. "houseName='" + houseName + '\'' +
  20. ", housePrice=" + housePrice +
  21. '}';
  22. }
  23. }

Person类:

  1. import java.util.List;
  2. public class Person {
  3. private String username;
  4. private Integer age;
  5. private Car car;
  6. private List<String> girlfriend;
  7. private List<House> house;
  8. public String getUsername() {
  9. return username;
  10. }
  11. public void setUsername(String username) {
  12. this.username = username;
  13. }
  14. public Integer getAge() {
  15. return age;
  16. }
  17. public void setAge(Integer age) {
  18. this.age = age;
  19. }
  20. public Car getCar() {
  21. return car;
  22. }
  23. public void setCar(Car car) {
  24. this.car = car;
  25. }
  26. public List<String> getGirlfriend() {
  27. return girlfriend;
  28. }
  29. public void setGirlfriend(List<String> girlfriend) {
  30. this.girlfriend = girlfriend;
  31. }
  32. public List<House> getHouse() {
  33. return house;
  34. }
  35. public void setHouse(List<House> house) {
  36. this.house = house;
  37. }
  38. @Override
  39. public String toString() {
  40. return "Person{" +
  41. "username='" + username + '\'' +
  42. ", age=" + age +
  43. ", car=" + car +
  44. ", girlfriend=" + girlfriend +
  45. ", house=" + house +
  46. '}';
  47. }
  48. }

测试类:

  1. import com.fasterxml.jackson.databind.ObjectMapper;
  2. import java.io.IOException;
  3. public class MyTest {
  4. public static void main(String[] args) throws IOException {
  5. //{"username":"王五","age":20,"car":{"carName":"宝马","carPrice":8888},"girlfriend":["刘亦菲","张曼玉"],"house":[{"houseName":"江滨花园",
  6. // "housePrice":50000},
  7. // {"houseName":"巴黎世家","housePrice":150000}]}
  8. String jsonStr = "{\"username\":\"王五\",\"age\":20,\"car\":{\"carName\":\"宝马\",\"carPrice\":8888},\"girlfriend\":[\"刘亦菲\",\"张曼玉\"]," +
  9. "\"house\":[{\"houseName\":\"江滨花园\",\"housePrice\":50000},{\"houseName\":\"巴黎世家\",\"housePrice\":150000}]}";
  10. ObjectMapper mapper = new ObjectMapper();
  11. Person person = mapper.readValue(jsonStr, Person.class);
  12. System.out.println(person);
  13. }
  14. }


7.天气JSON字符串转换Java对象

测试类:

  1. import com.fasterxml.jackson.databind.ObjectMapper;
  2. import java.io.IOException;
  3. public class MyTest {
  4. public static void main(String[] args) throws IOException {
  5. String jsonStr = "{\"data\":{\"yesterday\":{\"date\":\"20日星期四\",\"high\":\"高温 2℃\",\"fx\":\"东南风\",\"low\":\"低温 0℃\"," +
  6. "\"fl\":\"<![CDATA[2级]]>\",\"type\":\"霾\"},\"city\":\"商洛\",\"forecast\":[{\"date\":\"21日星期五\",\"high\":\"高温 0℃\"," +
  7. "\"fengli\":\"<![CDATA[2级]]>\",\"low\":\"低温 -1℃\",\"fengxiang\":\"东南风\",\"type\":\"小雪\"},{\"date\":\"22日星期六\",\"high\":\"高温 2℃\"," +
  8. "\"fengli\":\"<![CDATA[1级]]>\",\"low\":\"低温 -1℃\",\"fengxiang\":\"东南风\",\"type\":\"小雪\"},{\"date\":\"23日星期天\",\"high\":\"高温 1℃\"," +
  9. "\"fengli\":\"<![CDATA[1级]]>\",\"low\":\"低温 -2℃\",\"fengxiang\":\"东南风\",\"type\":\"小雪\"},{\"date\":\"24日星期一\",\"high\":\"高温 1℃\"," +
  10. "\"fengli\":\"<![CDATA[1级]]>\",\"low\":\"低温 -4℃\",\"fengxiang\":\"西北风\",\"type\":\"阴\"},{\"date\":\"25日星期二\",\"high\":\"高温 1℃\"," +
  11. "\"fengli\":\"<![CDATA[1级]]>\",\"low\":\"低温 -4℃\",\"fengxiang\":\"北风\",\"type\":\"阴\"}]," +
  12. "\"ganmao\":\"感冒多发期,适当减少外出频率,适量补充水分,适当增减衣物。\",\"wendu\":\"-1\"},\"status\":1000,\"desc\":\"OK\"}";
  13. ObjectMapper mapper = new ObjectMapper();
  14. TianQi tianQi = mapper.readValue(jsonStr, TianQi.class);
  15. String date = tianQi.getData().getYesterday().getDate();
  16. System.out.println(date);
  17. }
  18. }

TianQi类:

  1. @JsonIgnoreProperties(ignoreUnknown = true)
  2. public class TianQi {
  3. /**
  4. * data : {"yesterday":{"date":"20日星期四","high":"高温 2℃","fx":"东南风","low":"低温 0℃","fl":"<![CDATA[2级]]>","type":"霾"},"city":"商洛","forecast":[{
  5. * "date":"21日星期五","high":"高温 0℃","fengli":"<![CDATA[2级]]>","low":"低温 -1℃","fengxiang":"东南风","type":"小雪"},{"date":"22日星期六","high":"高温 2℃",
  6. * "fengli":"<![CDATA[1级]]>","low":"低温 -1℃","fengxiang":"东南风","type":"小雪"},{"date":"23日星期天","high":"高温 1℃","fengli":"<![CDATA[1级]]>","low":"低温
  7. * -2℃","fengxiang":"东南风","type":"小雪"},{"date":"24日星期一","high":"高温 1℃","fengli":"<![CDATA[1级]]>","low":"低温 -4℃","fengxiang":"西北风","type":"阴"},{
  8. * "date":"25日星期二","high":"高温 1℃","fengli":"<![CDATA[1级]]>","low":"低温 -4℃","fengxiang":"北风","type":"阴"}],"ganmao
  9. * ":"感冒多发期,适当减少外出频率,适量补充水分,适当增减衣物。","wendu":"-1"}
  10. * status : 1000
  11. * desc : OK
  12. */
  13. private DataBean data;
  14. private int status;
  15. private String desc;
  16. public DataBean getData() {
  17. return data;
  18. }
  19. public void setData(DataBean data) {
  20. this.data = data;
  21. }
  22. public int getStatus() {
  23. return status;
  24. }
  25. public void setStatus(int status) {
  26. this.status = status;
  27. }
  28. public String getDesc() {
  29. return desc;
  30. }
  31. public void setDesc(String desc) {
  32. this.desc = desc;
  33. }
  34. @JsonIgnoreProperties(ignoreUnknown = true)
  35. public static class DataBean {
  36. /**
  37. * yesterday : {"date":"20日星期四","high":"高温 2℃","fx":"东南风","low":"低温 0℃","fl":"<![CDATA[2级]]>","type":"霾"}
  38. * city : 商洛
  39. * forecast : [{"date":"21日星期五","high":"高温 0℃","fengli":"<![CDATA[2级]]>","low":"低温 -1℃","fengxiang":"东南风","type":"小雪"},{"date":"22日星期六",
  40. * "high":"高温 2℃","fengli":"<![CDATA[1级]]>","low":"低温 -1℃","fengxiang":"东南风","type":"小雪"},{"date":"23日星期天","high":"高温 1℃",
  41. * "fengli":"<![CDATA[1级]]>","low":"低温 -2℃","fengxiang":"东南风","type":"小雪"},{"date":"24日星期一","high":"高温 1℃","fengli":"<![CDATA[1级]]>",
  42. * "low":"低温 -4℃","fengxiang":"西北风","type":"阴"},{"date":"25日星期二","high":"高温 1℃","fengli":"<![CDATA[1级]]>","low":"低温 -4℃","fengxiang":"北风",
  43. * "type":"阴"}]
  44. * ganmao : 感冒多发期,适当减少外出频率,适量补充水分,适当增减衣物。
  45. * wendu : -1
  46. */
  47. private YesterdayBean yesterday;
  48. private String city;
  49. private String ganmao;
  50. private String wendu;
  51. private List<ForecastBean> forecast;
  52. public YesterdayBean getYesterday() {
  53. return yesterday;
  54. }
  55. public void setYesterday(YesterdayBean yesterday) {
  56. this.yesterday = yesterday;
  57. }
  58. public String getCity() {
  59. return city;
  60. }
  61. public void setCity(String city) {
  62. this.city = city;
  63. }
  64. public String getGanmao() {
  65. return ganmao;
  66. }
  67. public void setGanmao(String ganmao) {
  68. this.ganmao = ganmao;
  69. }
  70. public String getWendu() {
  71. return wendu;
  72. }
  73. public void setWendu(String wendu) {
  74. this.wendu = wendu;
  75. }
  76. public List<ForecastBean> getForecast() {
  77. return forecast;
  78. }
  79. public void setForecast(List<ForecastBean> forecast) {
  80. this.forecast = forecast;
  81. }
  82. @JsonIgnoreProperties(ignoreUnknown = true)
  83. public static class YesterdayBean {
  84. /**
  85. * date : 20日星期四
  86. * high : 高温 2℃
  87. * fx : 东南风
  88. * low : 低温 0℃
  89. * fl : <![CDATA[2级]]>
  90. * type : 霾
  91. */
  92. private String date;
  93. private String high;
  94. private String fx;
  95. private String low;
  96. private String fl;
  97. private String type;
  98. public String getDate() {
  99. return date;
  100. }
  101. public void setDate(String date) {
  102. this.date = date;
  103. }
  104. public String getHigh() {
  105. return high;
  106. }
  107. public void setHigh(String high) {
  108. this.high = high;
  109. }
  110. public String getFx() {
  111. return fx;
  112. }
  113. public void setFx(String fx) {
  114. this.fx = fx;
  115. }
  116. public String getLow() {
  117. return low;
  118. }
  119. public void setLow(String low) {
  120. this.low = low;
  121. }
  122. public String getFl() {
  123. return fl;
  124. }
  125. public void setFl(String fl) {
  126. this.fl = fl;
  127. }
  128. public String getType() {
  129. return type;
  130. }
  131. public void setType(String type) {
  132. this.type = type;
  133. }
  134. }
  135. @JsonIgnoreProperties(ignoreUnknown = true)
  136. public static class ForecastBean {
  137. /**
  138. * date : 21日星期五
  139. * high : 高温 0℃
  140. * fengli : <![CDATA[2级]]>
  141. * low : 低温 -1℃
  142. * fengxiang : 东南风
  143. * type : 小雪
  144. */
  145. private String date;
  146. private String high;
  147. private String fengli;
  148. private String low;
  149. private String fengxiang;
  150. private String type;
  151. public String getDate() {
  152. return date;
  153. }
  154. public void setDate(String date) {
  155. this.date = date;
  156. }
  157. public String getHigh() {
  158. return high;
  159. }
  160. public void setHigh(String high) {
  161. this.high = high;
  162. }
  163. public String getFengli() {
  164. return fengli;
  165. }
  166. public void setFengli(String fengli) {
  167. this.fengli = fengli;
  168. }
  169. public String getLow() {
  170. return low;
  171. }
  172. public void setLow(String low) {
  173. this.low = low;
  174. }
  175. public String getFengxiang() {
  176. return fengxiang;
  177. }
  178. public void setFengxiang(String fengxiang) {
  179. this.fengxiang = fengxiang;
  180. }
  181. public String getType() {
  182. return type;
  183. }
  184. public void setType(String type) {
  185. this.type = type;
  186. }
  187. }
  188. }
  189. }

总结:创建一个天气类,给IDEA中安装一个GsonFormat插件,就可以将复杂的JSON字符串转换成Java对象

 之后在新建类的页面,鼠标右键单击打开Generate,进去之后找到GsonFormat选项,进去之后把需要转换的JSON字符串粘贴进去,会自动生成我们想要的Java对象
进去之后点击左下角setting按键,设置好所用的jar包

 

最后就会将JSON字符串自动转换成一个Java对象了。

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发