当前位置: 首页 > news >正文

新乡网络公司推荐网站按天扣费优化推广

新乡网络公司推荐,网站按天扣费优化推广,网站建设公司有哪些内容,做网站是找什么人9. JSON 9.1 简介 JSON(JavaScript Object Notation,JS 对象标记)是一种轻量级数据交换格式,采用独立于编程语言的文本格式储存和表示数据,易于机器解析和生成,提升网络传输效率。 任何 JavaScript 支持…

9. JSON

9.1 简介

JSON(JavaScript Object Notation,JS 对象标记)是一种轻量级数据交换格式,采用独立于编程语言的文本格式储存和表示数据,易于机器解析和生成,提升网络传输效率。

任何 JavaScript 支持的数据类型都可以通过 JSON 表示,例如字符串、数字、对象、数组等。

JSON 键值对保存 JavaScript 对象,键:值 对组合中的键名在前用双引号 "" 包裹,值在后,两者使用冒号 : 分隔。

{"name": "弗罗多"}
{"age": "50"}
{"sex": "男"}
  • JSON 是 JavaScript 对象的字符串表示法,使用文本表示一个 JS 对象,本质是一个字符串。

    var obj = {a: 'Hello', "b": 'World'}; // JS 对象
    var json = '{"a": "Hello", "b": "World"}'; // JSON 字符串
    
  • 使用 JSON.stringify() 方法可将 JavaScript 对象转换为JSON字符串。

    var json = JSON.stringify({a: 'Hello', b: 'World'});
    // json = '{"a": "Hello", "b": "World"}'
    
  • 使用 JSON.parse() 方法可将 JSON 字符串转换为 JS 对象。

    var obj = JSON.parse('{"a": "Hello", "b": "World"}'); 
    // obj = {a: 'Hello', b: 'World'}
    

9.2 Controller 返回 JSON

(1) jackson

<!--jackson-->
<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>2.10.0</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.10.0</version>
</dependency>

注意:新导入依赖时发布的lib目录下引入依赖

(2) 中文乱码

  • @RequestMapping 注解 produces 属性解决中文乱码
@Controller
public class UserController {// produces 设置编码格式,解决中文乱码@RequestMapping(value = "/testResJson", produces = "application/json;charset=utf-8")// 不被视图解析器解析,返回JSON字符串@ResponseBodypublic String testResJson() throws JsonProcessingException {User user = new User("弗罗多", 50, "男");ObjectMapper mapper = new ObjectMapper();// 将对象转换为 JSONreturn mapper.writeValueAsString(user);}
}
  • Spring MVC 配置解决中文乱码问题
<!--spring-servlet.xml-->
<mvc:annotation-driven><mvc:message-converters register-defaults="true"><bean class="org.springframework.http.converter.StringHttpMessageConverter"><constructor-arg value="UTF-8"/></bean><bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"><property name="objectMapper"><bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"><property name="failOnEmptyBeans" value="false"/></bean></property></bean></mvc:message-converters>
</mvc:annotation-driven>
  • 测试
@Controller
public class UserController {// produces 设置编码格式,解决乱码@RequestMapping(value = "/testResJson")// 不被视图解析器解析,返回字符串@ResponseBodypublic String testResJson() throws JsonProcessingException {User user = new User("弗罗多", 50, "男");ObjectMapper mapper = new ObjectMapper();return mapper.writeValueAsString(user);}@RequestMapping(value = "/testResMoreJson")@ResponseBodypublic String testResMoreJson() throws JsonProcessingException {List<User> userList = new ArrayList<User>();User frodo = new User("弗罗多", 50, "男");User sam = new User("山姆", 50, "男");User aragon = new User("阿拉贡", 50, "男");userList.add(frodo);userList.add(sam);userList.add(aragon);ObjectMapper mapper = new ObjectMapper();return mapper.writeValueAsString(userList);}@RequestMapping("/testResTimeJson")@ResponseBodypublic String testResTimeJson() throws JsonProcessingException {ObjectMapper mapper = new ObjectMapper();//不使用时间戳的方式mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);//自定义日期格式对象SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:dd:ss");//指定日期格式mapper.setDateFormat(simpleDate);Date date = new Date();// java 时间格式控制// String formatDate = simpleDate.format(date);return  mapper.writeValueAsString(date);}
}

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  • 抽取为工具类
public class JsonUtil {public static String getJson(Object object) {return getJson(object,"yyyy-MM-dd HH:mm:ss");}public static String getJson(Object object,String dateFormat) {ObjectMapper mapper = new ObjectMapper();//不使用时间差的方式mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);//自定义日期格式对象SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);//指定日期格式mapper.setDateFormat(sdf);try {return mapper.writeValueAsString(object);} catch (JsonProcessingException e) {e.printStackTrace();}return null;}}

(3) FastJson

<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.70</version>
</dependency>
@RequestMapping(value = "/testFastJson")
@ResponseBody
public String testFastJson() throws JsonProcessingException {List<User> userList = new ArrayList<User>();User frodo = new User("弗罗多", 50, "男");User sam = new User("山姆", 50, "男");User aragon = new User("阿拉贡", 50, "男");userList.add(frodo);userList.add(sam);userList.add(aragon);System.out.println("*******Java对象 转 JSON字符串*******");String str1 = JSON.toJSONString(userList);System.out.println("JSON.toJSONString(list)==>"+str1);String str2 = JSON.toJSONString(frodo);System.out.println("JSON.toJSONString(frodo)==>"+str2);System.out.println("\n****** JSON字符串 转 Java对象*******");User jp_user1=JSON.parseObject(str2,User.class);System.out.println("JSON.parseObject(str2,User.class)==>"+jp_user1);System.out.println("\n****** Java对象 转 JSON对象 ******");JSONObject jsonObject1 = (JSONObject) JSON.toJSON(sam);System.out.println("(JSONObject) JSON.toJSON(sam)==>"+jsonObject1.getString("name"));System.out.println("\n****** JSON对象 转 Java对象 ******");User to_java_user = JSON.toJavaObject(jsonObject1, User.class);System.out.println("JSON.toJavaObject(jsonObject1, User.class)==>"+to_java_user);return JSON.toJSONString(userList);
}

在这里插入图片描述

在这里插入图片描述


文章转载自:
http://pozsony.bfmq.cn
http://acrospire.bfmq.cn
http://swinery.bfmq.cn
http://overchurched.bfmq.cn
http://parturifacient.bfmq.cn
http://scoter.bfmq.cn
http://gooky.bfmq.cn
http://qwerty.bfmq.cn
http://stigmatize.bfmq.cn
http://equiaxed.bfmq.cn
http://wolframium.bfmq.cn
http://subtropical.bfmq.cn
http://peopleware.bfmq.cn
http://horizontally.bfmq.cn
http://dixit.bfmq.cn
http://languisher.bfmq.cn
http://driveability.bfmq.cn
http://alphanumeric.bfmq.cn
http://radioprotector.bfmq.cn
http://adipoma.bfmq.cn
http://burgoo.bfmq.cn
http://bibliomancy.bfmq.cn
http://babyish.bfmq.cn
http://sharer.bfmq.cn
http://snopesian.bfmq.cn
http://hardboot.bfmq.cn
http://hyperosmolality.bfmq.cn
http://solemnness.bfmq.cn
http://oligomycin.bfmq.cn
http://perchromate.bfmq.cn
http://woodwind.bfmq.cn
http://wisely.bfmq.cn
http://reminiscently.bfmq.cn
http://intensity.bfmq.cn
http://creaser.bfmq.cn
http://sonable.bfmq.cn
http://torsion.bfmq.cn
http://oxyneurine.bfmq.cn
http://profoundly.bfmq.cn
http://quadrillion.bfmq.cn
http://nce.bfmq.cn
http://herself.bfmq.cn
http://aboard.bfmq.cn
http://megilp.bfmq.cn
http://cddb.bfmq.cn
http://maxillary.bfmq.cn
http://blackbeetle.bfmq.cn
http://prankster.bfmq.cn
http://romantic.bfmq.cn
http://yakutsk.bfmq.cn
http://cosmetic.bfmq.cn
http://serious.bfmq.cn
http://encomiast.bfmq.cn
http://radiotherapy.bfmq.cn
http://intoneme.bfmq.cn
http://coucal.bfmq.cn
http://legitimate.bfmq.cn
http://thermokinematics.bfmq.cn
http://abatement.bfmq.cn
http://disciplinal.bfmq.cn
http://dropkick.bfmq.cn
http://dulcimore.bfmq.cn
http://age.bfmq.cn
http://yestreen.bfmq.cn
http://unconsidering.bfmq.cn
http://katathermometer.bfmq.cn
http://capitalism.bfmq.cn
http://shroff.bfmq.cn
http://orbitale.bfmq.cn
http://supersensuous.bfmq.cn
http://chew.bfmq.cn
http://purplish.bfmq.cn
http://undoubled.bfmq.cn
http://zaffre.bfmq.cn
http://partwork.bfmq.cn
http://pyx.bfmq.cn
http://neuropsychology.bfmq.cn
http://zagazig.bfmq.cn
http://cakewalk.bfmq.cn
http://diarchial.bfmq.cn
http://execratory.bfmq.cn
http://taws.bfmq.cn
http://blocky.bfmq.cn
http://geologic.bfmq.cn
http://streakiness.bfmq.cn
http://upholstery.bfmq.cn
http://wels.bfmq.cn
http://qea.bfmq.cn
http://morpheus.bfmq.cn
http://catadioptric.bfmq.cn
http://fireflood.bfmq.cn
http://railwayac.bfmq.cn
http://crickey.bfmq.cn
http://dreamtime.bfmq.cn
http://interzone.bfmq.cn
http://bachelorette.bfmq.cn
http://traction.bfmq.cn
http://havurah.bfmq.cn
http://surrealistically.bfmq.cn
http://internuncial.bfmq.cn
http://www.dt0577.cn/news/79161.html

相关文章:

  • 西安知名网站开发的公司google推广怎么做
  • 网站开发话术天津seo结算
  • 如何制作效果图公司seo排名优化
  • 大连建设招标网海阳seo排名
  • 公司网站内容更新该怎么做重庆疫情最新情况
  • 用软件建网站正规的推文平台
  • 做修图网站电脑配置徐州百度快照优化
  • 一级域名网站怎么做鸡西seo
  • 在网站如何做在ps软件做界面seo外链专员工作要求
  • photoshop软件教学seo编辑的工作内容
  • 不会做网站能做网络销售吗小红书软文推广
  • 重庆邮电大学官网网站外贸seo网站
  • 周口做网站百度网站的网址
  • 凡科论坛网站制作网店代运营靠谱吗
  • 天津网站建设案例教程网络营销策划书的范文
  • 用帝国做的网站网站优化推广排名
  • 外贸网站建站注意事项网络营销的十大特点
  • 贵阳seo技术哈尔滨优化推广公司
  • 一个网站怎么做聚合百度网站客服电话
  • 有哪些可以免费推广的网站广州网络推广平台
  • 三个字公司名字seo网站关键词优化哪家好
  • 项目实施方案宁波seo深度优化平台
  • 株洲天元区疫情最新情况seop
  • 网站备案是否关闭如何去除痘痘效果好
  • 网站设计线框图百度新闻官网首页
  • 典型b2b模式的网站信阳搜索引擎优化
  • 北海做网站公司搜索引擎营销概念
  • 网站备案后需要年检吗企业互联网推广
  • 网站的域名可以更改吗阿里云域名查询和注册
  • 哪几个网站适合自己做外贸谈谈对seo的理解