什么是JSON?
十年的天山網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。全網(wǎng)營(yíng)銷推廣的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整天山建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)從事“天山網(wǎng)站設(shè)計(jì)”,“天山網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。JSON(JavaScript Object Notation:JS對(duì)象標(biāo)記)是一種輕量級(jí)的數(shù)據(jù)交換格式。使用廣泛,采用完全獨(dú)立于編程語(yǔ)言的文本格式來(lái)存儲(chǔ)和表示數(shù)據(jù)。簡(jiǎn)潔、清晰、易于閱讀、易于機(jī)器解析和生成、可以提升網(wǎng)絡(luò)傳輸效率。
Controller返回JSON數(shù)據(jù)
使用Jackson(一種json解析工具)
步驟:
1、導(dǎo)入jar包
com.fasterxml.jackson.core jackson-databind2.9.6
2、編寫web.xml
springmvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springmvc-servlet.xml 1 springmvc / encoding org.springframework.web.filter.CharacterEncodingFilter encoding utf-8 encoding /*
3、編寫springmvc-servlet.xml
4、編寫實(shí)體類
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private int age;
private String sex;
}
5、編寫一個(gè)Controller
@Controller
public class UserController {
@RequestMapping(value="/j1",produces = "application/json;charset=utf-8") //解決JSON亂碼問(wèn)題
@ResponseBody //它就不會(huì)走視圖解析器,會(huì)直接返回一個(gè)字符串
public String json1() throws JsonProcessingException {
User user = new User("張三",3,"男");
//把它轉(zhuǎn)換為JSON對(duì)象(字符串)
ObjectMapper mapper = new ObjectMapper();
String string = mapper.writeValueAsString(user);
return string;
}
}
亂碼統(tǒng)一解決
使用? produces = "application/json;charset=utf-8"? 來(lái)解決json亂碼問(wèn)題比較麻煩,如果項(xiàng)目中有很多請(qǐng)求,那么每個(gè)請(qǐng)求都要添加produces屬性。
可以通過(guò)Spring配置統(tǒng)一指定,這樣就不用每次都去處理了。
@Controller? ? ? ? 走視圖解析器
@RestController? ? ? ? 不走視圖解析器
@Controller + @ResponseBody = @RestController
1、把user對(duì)象轉(zhuǎn)換為JSON對(duì)象的顯示結(jié)果:
{"name":"張三","age":3,"sex":"男"}
2、把集合轉(zhuǎn)換為JSON對(duì)象
@RequestMapping(value="/j2")
@ResponseBody //它就不會(huì)走視圖解析器,會(huì)直接返回一個(gè)字符串
public String json2() throws JsonProcessingException {
ListuserList = new ArrayList();
User user1 = new User("張三",3,"男");
User user2 = new User("李四",4,"男");
User user3 = new User("王五",5,"男");
userList.add(user1);
userList.add(user2);
userList.add(user3);
//把它轉(zhuǎn)換為JSON對(duì)象(字符串)
ObjectMapper mapper = new ObjectMapper();
String string = mapper.writeValueAsString(userList);
return string;
}
顯示結(jié)果:
[{"name":"張三","age":3,"sex":"男"},{"name":"李四","age":4,"sex":"男"},{"name":"王五","age":5,"sex":"男"}]
3、把時(shí)間轉(zhuǎn)換為JSON對(duì)象
@RequestMapping(value="/j3")
@ResponseBody //它就不會(huì)走視圖解析器,會(huì)直接返回一個(gè)字符串
public String json3() throws JsonProcessingException {
Date date = new Date();
ObjectMapper mapper = new ObjectMapper();
//使用ObjectMapper解析時(shí)間后的默認(rèn)格式為:Timestamp(時(shí)間戳)
String string = mapper.writeValueAsString(date);
return string;
}
FastJson
fastjson.jar是阿里開(kāi)發(fā)的一款專門用于Java開(kāi)發(fā)的包,可以方便的實(shí)現(xiàn)json對(duì)象與JavaBean對(duì)象的轉(zhuǎn)換,實(shí)現(xiàn)JavaBean對(duì)象與json字符串的轉(zhuǎn)換,實(shí)現(xiàn)json對(duì)象與json字符串的轉(zhuǎn)換。實(shí)現(xiàn)json的轉(zhuǎn)換方法很多,最后的實(shí)現(xiàn)結(jié)果都是一樣的。
步驟:
1、導(dǎo)入依賴
com.alibaba fastjson1.2.60
fastjson中三個(gè)主要的類:
①JSONObject代表json對(duì)象
②JSONArray代表json對(duì)象數(shù)組
③JSON代表JSONObject和JSONArray的轉(zhuǎn)化
@RequestMapping(value="/j4")
@ResponseBody //它就不會(huì)走視圖解析器,會(huì)直接返回一個(gè)字符串
public void json4() throws JsonProcessingException {
ListuserList = new ArrayList();
User user1 = new User("張三",3,"男");
User user2 = new User("李四",4,"男");
User user3 = new User("王五",5,"男");
userList.add(user1);
userList.add(user2);
userList.add(user3);
//Java對(duì)象轉(zhuǎn)JSON字符串
String str1 = JSON.toJSONString(userList);
String str2 = JSON.toJSONString(user1);
System.out.println(str1);
System.out.println(str2);
//JSON字符串轉(zhuǎn)Java對(duì)象
User java_user = JSON.parseObject(str2,User.class);
System.out.println(java_user);
//Java對(duì)象轉(zhuǎn)JSON對(duì)象
JSONObject jsonObject1 = (JSONObject)JSON.toJSON(user2);
System.out.println(jsonObject1);
//JSON對(duì)象轉(zhuǎn)Java對(duì)象
User to_java_user = JSON.toJavaObject(jsonObject1, User.class);
System.out.println(to_java_user);
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧