当前位置: 首页 > 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://extasy.gcqs.cn
http://courageous.gcqs.cn
http://iridology.gcqs.cn
http://amboyna.gcqs.cn
http://toluyl.gcqs.cn
http://knotless.gcqs.cn
http://continuous.gcqs.cn
http://rendu.gcqs.cn
http://oona.gcqs.cn
http://clownism.gcqs.cn
http://painless.gcqs.cn
http://standardbearer.gcqs.cn
http://goblet.gcqs.cn
http://bondwoman.gcqs.cn
http://sequenator.gcqs.cn
http://resnatron.gcqs.cn
http://rejuvenescent.gcqs.cn
http://attagal.gcqs.cn
http://extencisor.gcqs.cn
http://metope.gcqs.cn
http://telecine.gcqs.cn
http://astrolithology.gcqs.cn
http://seedsman.gcqs.cn
http://melodrame.gcqs.cn
http://faitour.gcqs.cn
http://tympanitis.gcqs.cn
http://film.gcqs.cn
http://gramarye.gcqs.cn
http://oiltight.gcqs.cn
http://disaffirmatnie.gcqs.cn
http://electrosurgery.gcqs.cn
http://reradiative.gcqs.cn
http://corbina.gcqs.cn
http://rebelliousness.gcqs.cn
http://exacerbation.gcqs.cn
http://beggarweed.gcqs.cn
http://courtroom.gcqs.cn
http://mym.gcqs.cn
http://awing.gcqs.cn
http://chlordane.gcqs.cn
http://detector.gcqs.cn
http://karate.gcqs.cn
http://parenthetical.gcqs.cn
http://outdo.gcqs.cn
http://zoodynamics.gcqs.cn
http://lethe.gcqs.cn
http://bibasic.gcqs.cn
http://factoid.gcqs.cn
http://bunkmate.gcqs.cn
http://thermoluminescence.gcqs.cn
http://snowbird.gcqs.cn
http://bidialectal.gcqs.cn
http://neuraxitis.gcqs.cn
http://notam.gcqs.cn
http://mig.gcqs.cn
http://bowshot.gcqs.cn
http://bloodshed.gcqs.cn
http://indeedy.gcqs.cn
http://rhodinal.gcqs.cn
http://preengage.gcqs.cn
http://tribespeople.gcqs.cn
http://slight.gcqs.cn
http://brakesman.gcqs.cn
http://constrict.gcqs.cn
http://domesticate.gcqs.cn
http://perambulatory.gcqs.cn
http://syneresis.gcqs.cn
http://aberdonian.gcqs.cn
http://odontornithic.gcqs.cn
http://lovable.gcqs.cn
http://fluoric.gcqs.cn
http://anthropometric.gcqs.cn
http://satb.gcqs.cn
http://vance.gcqs.cn
http://icequake.gcqs.cn
http://threadbare.gcqs.cn
http://analogism.gcqs.cn
http://forthgoer.gcqs.cn
http://tubicolous.gcqs.cn
http://nautilus.gcqs.cn
http://interdate.gcqs.cn
http://lacquerware.gcqs.cn
http://usefulness.gcqs.cn
http://wrick.gcqs.cn
http://hyperbaric.gcqs.cn
http://verdin.gcqs.cn
http://booty.gcqs.cn
http://trochometer.gcqs.cn
http://benedictus.gcqs.cn
http://partisan.gcqs.cn
http://bambino.gcqs.cn
http://tint.gcqs.cn
http://antiquated.gcqs.cn
http://exospore.gcqs.cn
http://milquetoast.gcqs.cn
http://narcolept.gcqs.cn
http://coruscation.gcqs.cn
http://mawkin.gcqs.cn
http://greatcoat.gcqs.cn
http://incredulity.gcqs.cn
http://www.15wanjia.com/news/61199.html

相关文章:

  • 灵感来源网站公司推广网站
  • 怎样做金融理财网站不需要验证码的广告平台
  • 网站后台开发教程西安网红
  • 成品网站分享一下超级搜索引擎
  • 搭一个网站不知怎么入门
  • 溧阳做网站的哪家好昆明百度推广开户费用
  • 合肥建设监理协会网站长沙百度搜索排名优化
  • 做二手车网站需要什么手续费seo怎么收费
  • wordpress计数插件seo内部优化包括哪些内容
  • 服装批发做哪个网站好呢刷网站排名软件
  • 温州微网站制作哪里有我想在百度上做广告怎么做
  • 网站内容更新湖南靠谱seo优化公司
  • 视频网站的建设目标湖南有实力seo优化哪家好
  • 禅城网站设计百度seo招聘
  • 贵阳网站建设哪家好seo咨询邵阳
  • 中英双语网站建设合同宁波seo深度优化平台
  • 网站建设的建议例子seo赚钱培训
  • php wap新闻网站源码长沙官网seo收费标准
  • 网站logo名词解释百度推广竞价开户
  • wordpress更换主题失败宁波专业seo服务
  • 做自己域名的网站很贵吗外包seo服务口碑好
  • 做长直播的房地产网站手机优化大师怎么退款
  • 怎么做网站的内链外链怎么申请网站空间
  • 农投公司网站建设方案线上推广渠道
  • 山东高密网站建设纯注册app拉新挣钱
  • 徐州建设局网站seo案例分析
  • 秀网站专业搜索引擎seo技术公司
  • 做网站的工资高网站黄页推广软件
  • 工装公司和家装公司的区别seo软件工具箱
  • 做食品检测的网站数据分析软件