Json字符串和java对象的互转
为什么要转换
- json字符串是{…}中包裹的键值对,我们在前台与Java后台交互时,前台需要json数据,而后台需要字符串给对象属性设值。因此我们需要前后端交互时,将数据转换。
- 前端传输过来的数据,肯定对应后端的一个对象的属性,因此我们需要将数据解析,赋值给这个属性,并通过这个成员属性去数据库查找。
解析json字符串赋值给对象的成员变量
-
通过观察字符串,分析json字符串中都包含哪些类和成员变量
1. { "data": { "yesterday": { "date": "30日星期四", "high": "高温 30℃", "fx": "南风", "low": "低温 22℃", "fl": "<![CDATA[2级]]>", "type": "多云" }, "city": "西安", "forecast": [{ "date": "31日星期五", "high": "高温 32℃", "fengli": "<![CDATA[1级]]>", "low": "低温 23℃", "fengxiang": "东风", "type": "多云" }, { "date": "1日星期六", "high": "高温 33℃", "fengli": "<![CDATA[1级]]>", "low": "低温 22℃", "fengxiang": "东南风", "type": "多云" }, { "date": "2日星期天", "high": "高温 33℃", "fengli": "<![CDATA[1级]]>", "low": "低温 23℃", "fengxiang": "西南风", "type": "多云" }, { "date": "3日星期一", "high": "高温 31℃", "fengli": "<![CDATA[2级]]>", "low": "低温 25℃", "fengxiang": "东风", "type": "多云" }, { "date": "4日星期二", "high": "高温 32℃", "fengli": "<![CDATA[3级]]>", "low": "低温 26℃", "fengxiang": "东北风", "type": "阴" }], "ganmao": "感冒低发期,天气舒适,请注意多吃蔬菜水果,多喝水哦。", "wendu": "27" }, "status": 1000, "desc": "OK" } 2.//先分析里面包含的类,并创建此类 package org.westos.demo; /*Author:LH CreatTime:2020.07.30.15:31*/ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import java.util.List; @JsonIgnoreProperties(ignoreUnknown = true) public class Weather { /** * data : {"yesterday":{"date":"29日星期三","high":"高温 32℃","fx":"东风","low":"低温 21℃","fl":"<![CDATA[2级]]>","type":"多云"},"city":"西安","forecast":[{"date":"30日星期四","high":"高温 33℃","fengli":"<![CDATA[2级]]>","low":"低温 22℃","fengxiang":"东南风","type":"小雨"},{"date":"31日星期五","high":"高温 30℃","fengli":"<![CDATA[1级]]>","low":"低温 22℃","fengxiang":"东风","type":"小雨"},{"date":"1日星期六","high":"高温 31℃","fengli":"<![CDATA[1级]]>","low":"低温 22℃","fengxiang":"南风","type":"多云"},{"date":"2日星期天","high":"高温 34℃","fengli":"<![CDATA[2级]]>","low":"低温 25℃","fengxiang":"西南风","type":"多云"},{"date":"3日星期一","high":"高温 34℃","fengli":"<![CDATA[2级]]>","low":"低温 25℃","fengxiang":"东南风","type":"多云"}],"ganmao":"感冒易发期,外出请适当调整衣物,注意补充水分。","wendu":"33"} * status : 1000 * desc : OK */ private DataBean data; private int status; private String desc; public DataBean getData() { return data; } public void setData(DataBean data) { this.data = data; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } @JsonIgnoreProperties(ignoreUnknown = true) public static class DataBean { /** * yesterday : {"date":"29日星期三","high":"高温 32℃","fx":"东风","low":"低温 21℃","fl":"<![CDATA[2级]]>","type":"多云"} * city : 西安 * forecast : [{"date":"30日星期四","high":"高温 33℃","fengli":"<![CDATA[2级]]>","low":"低温 22℃","fengxiang":"东南风","type":"小雨"},{"date":"31日星期五","high":"高温 30℃","fengli":"<![CDATA[1级]]>","low":"低温 22℃","fengxiang":"东风","type":"小雨"},{"date":"1日星期六","high":"高温 31℃","fengli":"<![CDATA[1级]]>","low":"低温 22℃","fengxiang":"南风","type":"多云"},{"date":"2日星期天","high":"高温 34℃","fengli":"<![CDATA[2级]]>","low":"低温 25℃","fengxiang":"西南风","type":"多云"},{"date":"3日星期一","high":"高温 34℃","fengli":"<![CDATA[2级]]>","low":"低温 25℃","fengxiang":"东南风","type":"多云"}] * ganmao : 感冒易发期,外出请适当调整衣物,注意补充水分。 * wendu : 33 */ private YesterdayBean yesterday; private String city; private String ganmao; private String wendu; private List<ForecastBean> forecast; public YesterdayBean getYesterday() { return yesterday; } public void setYesterday(YesterdayBean yesterday) { this.yesterday = yesterday; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getGanmao() { return ganmao; } public void setGanmao(String ganmao) { this.ganmao = ganmao; } public String getWendu() { return wendu; } public void setWendu(String wendu) { this.wendu = wendu; } public List<ForecastBean> getForecast() { return forecast; } public void setForecast(List<ForecastBean> forecast) { this.forecast = forecast; } @JsonIgnoreProperties(ignoreUnknown = true) public static class YesterdayBean { /** * date : 29日星期三 * high : 高温 32℃ * fx : 东风 * low : 低温 21℃ * fl : <![CDATA[2级]]> * type : 多云 */ private String date; private String high; private String fx; private String low; private String fl; private String type; public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getHigh() { return high; } public void setHigh(String high) { this.high = high; } public String getFx() { return fx; } public void setFx(String fx) { this.fx = fx; } public String getLow() { return low; } public void setLow(String low) { this.low = low; } public String getFl() { return fl; } public void setFl(String fl) { this.fl = fl; } public String getType() { return type; } public void setType(String type) { this.type = type; } } @JsonIgnoreProperties(ignoreUnknown = true) public static class ForecastBean { /** * date : 30日星期四 * high : 高温 33℃ * fengli : <![CDATA[2级]]> * low : 低温 22℃ * fengxiang : 东南风 * type : 小雨 */ private String date; private String high; private String fengli; private String low; private String fengxiang; private String type; public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getHigh() { return high; } public void setHigh(String high) { this.high = high; } public String getFengli() { return fengli; } public void setFengli(String fengli) { this.fengli = fengli; } public String getLow() { return low; } public void setLow(String low) { this.low = low; } public String getFengxiang() { return fengxiang; } public void setFengxiang(String fengxiang) { this.fengxiang = fengxiang; } public String getType() { return type; } public void setType(String type) { this.type = type; } } } } 3.然后使用jackson解析器,将json字符串解析成对象。 ObjectMapper mapper = new ObjectMapper(); String data="{\"data\":{\"yesterday\":{\"date\":\"29日星期三\",\"high\":\"高温 32℃\",\"fx\":\"东风\",\"low\":\"低温 21℃\",\"fl\":\"<![CDATA[2级]]>\",\"type\":\"多云\"},\"city\":\"西安\",\"forecast\":[{\"date\":\"30日星期四\",\"high\":\"高温 33℃\",\"fengli\":\"<![CDATA[2级]]>\",\"low\":\"低温 22℃\",\"fengxiang\":\"东南风\",\"type\":\"小雨\"},{\"date\":\"31日星期五\",\"high\":\"高温 30℃\",\"fengli\":\"<![CDATA[1级]]>\",\"low\":\"低温 22℃\",\"fengxiang\":\"东风\",\"type\":\"小雨\"},{\"date\":\"1日星期六\",\"high\":\"高温 31℃\",\"fengli\":\"<![CDATA[1级]]>\",\"low\":\"低温 22℃\",\"fengxiang\":\"南风\",\"type\":\"多云\"},{\"date\":\"2日星期天\",\"high\":\"高温 34℃\",\"fengli\":\"<![CDATA[2级]]>\",\"low\":\"低温 25℃\",\"fengxiang\":\"西南风\",\"type\":\"多云\"},{\"date\":\"3日星期一\",\"high\":\"高温 34℃\",\"fengli\":\"<![CDATA[2级]]>\",\"low\":\"低温 25℃\",\"fengxiang\":\"东南风\",\"type\":\"多云\"}],\"ganmao\":\"感冒易发期,外出请适当调整衣物,注意补充水分。\",\"wendu\":\"33\"},\"status\":1000,\"desc\":\"OK\"}"; // 将json转成对象 //需要两个值, //参数1:需要解析的json字符串 //参数2:json字符串映射的类的class文件。也就是解析出来的数据对应的类的字节码文件 Weather weather = mapper.readValue(data, Weather.class); System.out.println(weather.getData().getCity());
-
通常情况json字符串比较繁琐,我们使用插件生成他对应的类
-
以上步骤完毕之后根据json的属性来选择类的属性,创建类
解析器
- 解析json和将对象转成json字符串,我们使用的是解析器
- 常见的解析器: GSON FastJSON jackson
- 我们使用jackson解析器,上面使用到了jackson解析器,将json字符串转换成了对象属性。
- 这个解析器是第三方应用,我们需要将jar包导入并依赖才能使用。
- 然后调取方法,就可以实现jsonzi字符串与对象之间互转。
- 对象转json对象
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
public class mytest {
public static void main(String[] args) throws IOException {
Person person = new Person();
person.setName("zhangsan");
person.setAge("12");
person.setHeight(167);
ObjectMapper mapper = new ObjectMapper();
// 将对象转成json
String s = mapper.writeValueAsString(person);
System.out.println(s);
}
}