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

咸阳免费做网站网站流量查询工具

咸阳免费做网站,网站流量查询工具,怎么建设公司网站信息,手机网站前端开发布局技巧目录 FastJson 新建一个SpringBoot项目 pom.xml 一、JavaBean与JSON数据相互转换 LoginController FastJsonApplication启动类 ​编辑二、FastJson的JSONField注解 Log实体类 TestLog测试类 三、FastJson对JSON数据的增、删、改、查 TestCrud FastJson 1、JSON使用手册…

目录

FastJson

新建一个SpringBoot项目

pom.xml

一、JavaBean与JSON数据相互转换

LoginController

 FastJsonApplication启动类

​编辑二、FastJson的@JSONField注解

Log实体类

 TestLog测试类

三、FastJson对JSON数据的增、删、改、查

TestCrud


FastJson

  • 1、JSON使用手册:JSON 教程 | 菜鸟教程 (runoob.com)
  • 2、FastJson官方文档:Quick Start CN · alibaba/fastjson Wiki (github.com)
  • 3、
  • JSON(JavaScript Object Notation, JavaScript 对象标记法),是一种轻量级的数据交换格式。
  • 对于一个前后端分离的SpringBoot项目而言,前端需要的是以“键:值”结构保存的JSON数据,后端需要的是JavaBean。所以出现了两种JSON解析库,把它们转来转去,以便前后端进行数据交流:
    • 1、Spring Boot内置的Jackson(适合场景复杂、业务量大的项目)
    • 2、阿里巴巴开发的FastJson(适合数据量小、并发量小的项目)
  • FastJson是JSON解析库,用于转换JavaBean和JSON数据
    • 序列化(将Java对象转换为JSON字符串)
    • 反序列化(将JSON字符串转换为Java对象)

    • String text = JSON.toJSONString(obj); //序列化
      VO vo = JSON.parseObject("{...}", VO.class); //反序列化
      //VO:与JSON数据对应的实体类
  • @JSONField注解:
    • 当你需要更精确地控制Java对象的字段在序列化和反序列化过程中的行为时,可以使用@JSONField注解
    • @JSONField注解可以用于声明类、属性或方法
    • 该注解可以让人重新定制序列化规则 
  • 增删改查:
    • FastJSON将JSON数据分成“对象”和“数组”两种形式,
    • 把对象节点封装成JSONObject类,
    • 把数组节点封装成JSONArray类,
    • 然后利用这两个类对JSON数据进行增、删、改查操作

新建一个SpringBoot项目

 

项目结构:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.12.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.study</groupId><artifactId>fastJson</artifactId><version>0.0.1-SNAPSHOT</version><name>fastJson</name><description>Demo project for Spring Boot</description><properties><java.version>8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--添加FastJSON依赖--><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.28</version></dependency><!--使用@Test注解--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

一、JavaBean与JSON数据相互转换

LoginController

  • 接收前端发来的JSON数据,返回JSON登录结果
package com.study.fastJson.controller;import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;
import java.util.Map;/*** 接收前端发来的JSON数据,返回JSON登录结果*/
@RestController
public class LoginController {@RequestMapping("/login")public String login(@RequestBody String json){//将请求体中的字符串以JSON格式读取并转换为Map键值对象Map loginDate=JSON.parseObject(json,Map.class);//读取JSON中的账号String username=loginDate.get("username").toString();//读取JSON中的密码String password=loginDate.get("password").toString();HashMap<String, String> result = new HashMap<>();//返回的响应码String code="";//返回的响应信息String message="";if("mr".equals(username) && "123456".equals(password)){code="200";message="登录成功";}else{code="500";message="账号或密码错误";}//将响应码和响应信息保存到result响应结果中result.put("code",code);result.put("message",message);//将键值对象转换为以"键:值"结构保存的JSON数据并返回return JSON.toJSONString(result);}
}

 FastJsonApplication启动类

package com.study.fastJson;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class FastJsonApplication {public static void main(String[] args) {SpringApplication.run(FastJsonApplication.class, args);}}

 启动启动类,使用postman进行测试

二、FastJson的@JSONField注解

Log实体类

@JSONField注解的几个重要属性:

  • name
  • serialize
  • format
  • ordinal
package com.study.fastJson.entity;import com.alibaba.fastjson.annotation.JSONField;import java.util.Date;/*** @JSONField注解的各种常见用法*/
public class Log {//ordinal用于定义不同属性被转换后的JSON数据中的排列顺序,值越大越靠后@JSONField(ordinal = 0,name="code")//为该属性定义别名"code"private String id;@JSONField(ordinal = 1,serialize = false)//该属性不会被序列化,即不显示public String message;@JSONField(ordinal = 2,format = "yyyy-MM-dd HH:mm:ss")//定义该属性序列化时的日期格式public Date create;public Log() {}public Log(String message,  Date create, String id) {this.message = message;this.create = create;this.id = id;}//Getter(),Setter()方法省略
}

 TestLog测试类

package com.study.fastJson.entity;import com.alibaba.fastjson.JSON;
import org.junit.Test;import java.util.Date;public class TestLog {/***  @JSONField(name="code")* 定义属性的别名,以别名使用该属性*/@Testpublic void testName(){Log log = new Log();log.setId("404");System.out.println(JSON.toJSONString(log));}/*** @JSONField(format = "yyyy-MM-dd HH:mm:ss")* 当Log对象被转换为JSON数据时,会自动按照@JSONField注解定义的日期格式进行转换*/@Testpublic void testDateFormat(){Log log = new Log();log.create=new Date();System.out.println(JSON.toJSONString(log));}/*** @JSONField(serialize = false)* id属性不会被序列化*/@Testpublic void testSerialize(){Log log = new Log();log.setId("404");log.message="找不到资源";System.out.println(JSON.toJSONString(log));}/***  @JSONField(ordinal = 0)*  ordinal用于定义不同属性被转换后的JSON数据中的排列顺序,值越大越靠后*/@Testpublic void testOrder(){Log log = new Log("找不到资源",new Date(),"404");System.out.println(JSON.toJSONString(log));}
}

三、FastJson对JSON数据的增、删、改、查

TestCrud

package com.study.fastJson;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;/*** 使用JSONObject类和JSONArray类对JSON数据进行增删改查操作*/
public class TestCrud {/*** 查询数据* 使用get()方法查询*/@Testpublic void getJson(){String json="{\"name\":\"张三\",\"age\":25,\"qq\":[\"123456789\",\"987654321\"],"+"\"scores\":{\"chinese\":90,\"math\":85}}";//获取以"键:值"结构保存的JSON数据的对象节点JSONObject root= JSON.parseObject(json);//查询指定字段对应的值String name=root.getString("name");int age=root.getIntValue("age");System.out.println("姓名:"+name+",年龄:"+age);//获取JSON数组中的值JSONArray arr=root.getJSONArray("qq");String firstQQ=arr.getString(0);System.out.println(firstQQ);//获取JSON子节点中的数据JSONObject scores=root.getJSONObject("scores");int math=scores.getIntValue("math");System.out.println("数学成绩为:"+math);}/*** 增加数据* 对象节点使用put()节点增加,数组节点使用add()方法增加*/@Testpublic void addJson(){String json="{\"name\":\"张三\",\"age\":25,\"qq\":[\"123456789\",\"987654321\"],"+"\"scores\":{\"chinese\":90,\"math\":85}}";//获取以"键:值"结构保存的JSON数据的对象节点JSONObject root= JSON.parseObject(json);root.put("sex","男");root.getJSONArray("qq").add("999999");root.getJSONObject("scores").put("english",92);System.out.println(root.toJSONString());}/*** 修改数据* 对象节点使用put()方法修改,数组节点使用set()方法修改*/@Testpublic void updateJson(){String json="{\"name\":\"张三\",\"age\":25,\"qq\":[\"123456789\",\"987654321\"],"+"\"scores\":{\"chinese\":90,\"math\":85}}";//获取以"键:值"结构保存的JSON数据的对象节点JSONObject root= JSON.parseObject(json);root.put("name","李四");//名字改成李四root.getJSONArray("qq").set(1,"000000");//将第二个qq号改成000000root.getJSONObject("scores").put("math",70);//数学成绩改成70System.out.println(root.toJSONString());}/*** 删除数据* 对象节点和数组节点都使用remove()方法删除*/@Testpublic void removeJson(){String json="{\"name\":\"张三\",\"age\":25,\"qq\":[\"123456789\",\"987654321\"],"+"\"scores\":{\"chinese\":90,\"math\":85}}";//获取以"键:值"结构保存的JSON数据的对象节点JSONObject root= JSON.parseObject(json);root.remove("age");//删除年龄字段root.getJSONArray("qq").remove(0);//删除第一个qq号root.getJSONObject("scores").remove("chinese");//删除语文成绩System.out.println(root.toJSONString());}
}

文章转载自:
http://dinncophlox.tqpr.cn
http://dinncohandweaving.tqpr.cn
http://dinncoknickerbockers.tqpr.cn
http://dinncostoreship.tqpr.cn
http://dinncosubsensible.tqpr.cn
http://dinncogalabia.tqpr.cn
http://dinncounauthoritative.tqpr.cn
http://dinncotampax.tqpr.cn
http://dinncounderstanding.tqpr.cn
http://dinncoshopkeeper.tqpr.cn
http://dinncofrat.tqpr.cn
http://dinncoromanticism.tqpr.cn
http://dinncoepithet.tqpr.cn
http://dinncoenvirons.tqpr.cn
http://dinncopavement.tqpr.cn
http://dinncohaulage.tqpr.cn
http://dinncotoupee.tqpr.cn
http://dinncoanastatic.tqpr.cn
http://dinncosense.tqpr.cn
http://dinncohechima.tqpr.cn
http://dinncoschizonticide.tqpr.cn
http://dinncogangbuster.tqpr.cn
http://dinncoturcophobe.tqpr.cn
http://dinncotone.tqpr.cn
http://dinncocircumspectly.tqpr.cn
http://dinncoetesian.tqpr.cn
http://dinncoflusteration.tqpr.cn
http://dinncograduation.tqpr.cn
http://dinncognomon.tqpr.cn
http://dinncouncinus.tqpr.cn
http://dinncofiendish.tqpr.cn
http://dinncounhurriedly.tqpr.cn
http://dinncouncinate.tqpr.cn
http://dinncorubbly.tqpr.cn
http://dinncologotherapy.tqpr.cn
http://dinncoaerenchyma.tqpr.cn
http://dinncobungaloid.tqpr.cn
http://dinncobravery.tqpr.cn
http://dinncoclericalist.tqpr.cn
http://dinncomonotonize.tqpr.cn
http://dinncoselenodesy.tqpr.cn
http://dinncoswizz.tqpr.cn
http://dinncogiraffine.tqpr.cn
http://dinncoseance.tqpr.cn
http://dinncotheurgist.tqpr.cn
http://dinncoinsecurity.tqpr.cn
http://dinncokeratode.tqpr.cn
http://dinncorevanchism.tqpr.cn
http://dinncosolenodon.tqpr.cn
http://dinncoowner.tqpr.cn
http://dinncobagel.tqpr.cn
http://dinncobec.tqpr.cn
http://dinncogelatine.tqpr.cn
http://dinncoundisguisedly.tqpr.cn
http://dinncoscission.tqpr.cn
http://dinncowaxweed.tqpr.cn
http://dinncofishtail.tqpr.cn
http://dinncoacyloin.tqpr.cn
http://dinncourochrome.tqpr.cn
http://dinncosmudgy.tqpr.cn
http://dinncoindagator.tqpr.cn
http://dinncosaleyard.tqpr.cn
http://dinnconifelheim.tqpr.cn
http://dinncoanglo.tqpr.cn
http://dinncowhalehead.tqpr.cn
http://dinncorosebud.tqpr.cn
http://dinncomohammed.tqpr.cn
http://dinncogroundhog.tqpr.cn
http://dinncononprescription.tqpr.cn
http://dinncotriennially.tqpr.cn
http://dinncohispanism.tqpr.cn
http://dinncodelate.tqpr.cn
http://dinncotweet.tqpr.cn
http://dinncomisjoinder.tqpr.cn
http://dinnconauseous.tqpr.cn
http://dinncoadrienne.tqpr.cn
http://dinncofunnelform.tqpr.cn
http://dinncodiphenylchlorarsine.tqpr.cn
http://dinncoisosceles.tqpr.cn
http://dinncolithographer.tqpr.cn
http://dinncorencontre.tqpr.cn
http://dinncooverstrength.tqpr.cn
http://dinncoamati.tqpr.cn
http://dinnconovio.tqpr.cn
http://dinncobible.tqpr.cn
http://dinncoanesthesiology.tqpr.cn
http://dinncoriant.tqpr.cn
http://dinncocommonweal.tqpr.cn
http://dinncolymphoid.tqpr.cn
http://dinncoproteiform.tqpr.cn
http://dinncomillwork.tqpr.cn
http://dinncoundose.tqpr.cn
http://dinncocargo.tqpr.cn
http://dinncopartwork.tqpr.cn
http://dinncopalaeethnology.tqpr.cn
http://dinncobeadswoman.tqpr.cn
http://dinncodetraction.tqpr.cn
http://dinncostrapontin.tqpr.cn
http://dinncomegashear.tqpr.cn
http://dinncoavram.tqpr.cn
http://www.dinnco.com/news/156032.html

相关文章:

  • 凤凰县政府网站建设推广平台都有哪些
  • 优秀企业网站制作最好用的搜索引擎排名
  • 网站有什么到期个人博客搭建
  • 男生做男生网站在那看谷歌seo排名技巧
  • 动态网页怎么写seo关键词外包公司
  • 做公益筹集项目的网站百度seo点击排名优化
  • 江苏建设人才考试网官方网站网站自然排名工具
  • 网站制作难点建立网站用什么软件
  • 公司内部 网站开发网络营销技巧和营销方法
  • 网站服务费做管理费用国内重大新闻
  • 硬件开发需求seo职业规划
  • 网站建设近义词设计网站排行
  • 做网站起什么名字比较好下载百度语音导航地图
  • 自己搭建vps上外网苏州关键词seo排名
  • 扁平化设计风格的网站百度指数批量
  • 嘉定华亭网站建设指数基金定投技巧
  • 政府网站建设通知品牌营销策略四种类型
  • 网站如何与支付宝对接seo网站收录工具
  • 网站登录不了刷百度关键词排名
  • 做纪录片卖给视频网站网站seo优化网站
  • 企业网站建设费属于办公费吗免费的域名和网站
  • 做封面网站百度推广效果怎样
  • 服装网站建设目标网络营销外包推广价格
  • 做公司网站有什么亮点宝鸡网站seo
  • 广饶网站定制谷歌seo网站排名优化
  • 竞价在什么网站上做把百度网址大全设为首页
  • 做网站work什肇庆网站制作软件
  • 网站建设徐州百度网络网站石家庄seo推广
  • 宁波做网站价格百度词条优化
  • 美国做汽车配件的网站营销网站定制