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

朝阳网站建设推广成都网站制作设计公司

朝阳网站建设推广,成都网站制作设计公司,天津企业网站建设公司,电脑制作h5最常用软件一、Java中json字符串与json对象 1、json本质 json是一种数据交换格式。 常说的json格式的字符串 > 发送和接收时都只是一个字符串,它遵循json这种格式。 2、前后端交互传输的json是什么? 前后端交互传输的json都是json字符串 比如:…

一、Java中json字符串与json对象

1、json本质

json是一种数据交换格式。

常说的json格式的字符串 ==> 发送和接收时都只是一个字符串,它遵循json这种格式。

2、前后端交互传输的json是什么?

前后端交互传输的json都是json字符串

比如:

2.1响应过程:后端响应的实际上是json字符串(原理:Springboot中,关于为什么controller返回前端数据是json格式的思考_为什么我返回result中有一个对象,能返回成json-CSDN博客),前端axios接收的也是字符串,只不过自动帮我们转成了json对象。

2.2请求过程:前端传输的实际上也是json字符串,由后端的Jackson帮我们转成了对应的对象。

3、前端向后端传递json需要注意的点

3.1前端传过来的若是字符串格式的json字符串,则用JSONObject.parseArray()或者JSONObject.parseObject()转换。示例:
 前端传值:

 后端接收:

@PostMapping("/controlResponse")
public Result callBackResponse(@RequestBody String string){List<CallBackResponseDto> list= JSONObject.parseArray(string, CallBackResponseDto.class);return ResultInfo.success();
}//或者如下用object接收
@PostMapping("/controlResponse")
public ResultInfo callBackResponse(@RequestBody Object object) throws Exception {List<CallBackResponseDto> list = JSONObject.parseArray(object.toString(), CallBackResponseDto.class);return ResultInfo.success();
}

3.2若前端传过来的是对象格式的json字符串,则要先用JSON.toJSONString()将object转成字符串,然后用JSONObject.parseArray()或者JSONObject.parseObject()转换。示例:

前端传值:

 后端接收:

@PostMapping("/controlResponse")
public ResultInfo callBackResponse(@RequestBody Object object) throws Exception {List<CallBackResponseDto> list = JSONObject.parseArray(JSON.toJSONString(object), CallBackResponseDto.class);return ResultInfo.success();
}

 二、fastjson中遇到的问题

1、FastJson下划线转驼峰

1.1 parseObject(字符串转对象)的时候 设置下划线转驼峰

ParserConfig.getGlobalInstance().propertyNamingStrategy = PropertyNamingStrategy.SnakeCase;
JSON.parseObject(str, RespOrderDTO.class);

1.2 toJSONString(对象转字符串)的时候,设置驼峰转下划线

SerializeConfig config = new SerializeConfig();
config.propertyNamingStrategy = PropertyNamingStrategy.SnakeCase;
String json = JSON.toJSONString(user, config);

1.3 单个字段 设置下划线转驼峰

@JSONField(name="user_name")
private String userName;

注意:fastjson会反序列化时会智能匹配,fastjson2不会智能匹配

fastjson 2 大小写以及下划线问题_问答-阿里云开发者社区

Java中@JSONField和@JsonProperty注解的用法及区别详解_java_脚本之家

 2、Fastjson首字母大小写问题

 2.1、问题

使用Fastjson转json之后发现首字母小写。实体类如下:

@Data
public class DataIdentity {private String BYDBSM;private String SNWRSSJSJ;private Integer CJFS = 20;
}

测试代码如下:

public static void main(String[] args) {DataIdentity dataIdentity = new DataIdentity();dataIdentity.setBYDBSM("xxx");dataIdentity.setSNWRSSJSJ(DateUtil.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss"));String str = JSON.toJSONString(dataIdentity);System.out.println(str);
}

测试结果如下:

2.2、分析

通过查看Fastjson源码可知,Fatjson在序列化对象时,会判断compatibleWithJavaBean,如果为false则将首字母小写,compatibleWithJavaBean默认值为false.

public class TypeUtils {private static final Pattern NUMBER_WITH_TRAILING_ZEROS_PATTERN = Pattern.compile("\\.0*$");public static boolean compatibleWithJavaBean = false;public static boolean compatibleWithFieldName = false;
...
}
...
if (Character.isUpperCase(c2)) {if (compatibleWithJavaBean) {propertyName = decapitalize(methodName.substring(2));} else {propertyName = Character.toLowerCase(methodName.charAt(2)) + methodName.substring(3);}
propertyName = getPropertyNameByCompatibleFieldName(fieldCacheMap, methodName, propertyName, 2);
...

2.3、解决方案

1.compatibleWithJavaBean设置为true

TypeUtils.compatibleWithJavaBean = true;

也可以通过设置jvm参数。

2.@JSONField注解

@Data
public class DataIdentity {@JSONField(name = "BYDBSM")private String BYDBSM;@JSONField(name = "SNWRSSJSJ")private String SNWRSSJSJ;@JSONField(name = "CJFS")private Integer CJFS = 20;}

3、使用hutool的JSONUtil.toJsonStr()方法

String str = JSONUtil.toJsonStr(yytStuCountDto);

2.4、参考文章

Fastjson首字母大小写问题_fastjson 首字母小写-CSDN博客

BUG实录 | 第一篇:FastJSON转换Oracle字段成实体后首字母小写问题 - 有来技术 - 博客园

fastjson转换json时,碰到的那些首字母大小写转换的坑! - 简书


文章转载自:
http://rememberable.yzkf.cn
http://mikado.yzkf.cn
http://gelati.yzkf.cn
http://zarzuela.yzkf.cn
http://distemper.yzkf.cn
http://multimer.yzkf.cn
http://undissociated.yzkf.cn
http://ceterisparibus.yzkf.cn
http://disturbingly.yzkf.cn
http://ergal.yzkf.cn
http://fortaleza.yzkf.cn
http://insufflation.yzkf.cn
http://mirthless.yzkf.cn
http://renvoi.yzkf.cn
http://microinterrupt.yzkf.cn
http://bicommunal.yzkf.cn
http://intimidator.yzkf.cn
http://bac.yzkf.cn
http://unpack.yzkf.cn
http://compassable.yzkf.cn
http://tricolored.yzkf.cn
http://psychodynamic.yzkf.cn
http://fley.yzkf.cn
http://isobutylene.yzkf.cn
http://trimphone.yzkf.cn
http://kinetophonograph.yzkf.cn
http://sdrs.yzkf.cn
http://bounce.yzkf.cn
http://smartless.yzkf.cn
http://pusan.yzkf.cn
http://induce.yzkf.cn
http://larvivorous.yzkf.cn
http://waterlogged.yzkf.cn
http://cryptococcosis.yzkf.cn
http://catastrophist.yzkf.cn
http://payer.yzkf.cn
http://utopianism.yzkf.cn
http://intercommunicate.yzkf.cn
http://emend.yzkf.cn
http://ippon.yzkf.cn
http://translatability.yzkf.cn
http://remedially.yzkf.cn
http://midget.yzkf.cn
http://demodulate.yzkf.cn
http://djellaba.yzkf.cn
http://doily.yzkf.cn
http://exponential.yzkf.cn
http://monthly.yzkf.cn
http://wankel.yzkf.cn
http://decamp.yzkf.cn
http://papuan.yzkf.cn
http://annonaceous.yzkf.cn
http://volatilize.yzkf.cn
http://backscratching.yzkf.cn
http://ratton.yzkf.cn
http://itching.yzkf.cn
http://eristic.yzkf.cn
http://basswood.yzkf.cn
http://nonofficial.yzkf.cn
http://perceptional.yzkf.cn
http://flax.yzkf.cn
http://extortionate.yzkf.cn
http://sinnerite.yzkf.cn
http://tyrannous.yzkf.cn
http://ptyalism.yzkf.cn
http://tongueless.yzkf.cn
http://aneurin.yzkf.cn
http://backscratcher.yzkf.cn
http://luminal.yzkf.cn
http://watchtower.yzkf.cn
http://yoking.yzkf.cn
http://mercy.yzkf.cn
http://submitochondrial.yzkf.cn
http://terminator.yzkf.cn
http://brachistochrone.yzkf.cn
http://vitrescence.yzkf.cn
http://stiffener.yzkf.cn
http://imprest.yzkf.cn
http://narrater.yzkf.cn
http://amphichroic.yzkf.cn
http://flagellant.yzkf.cn
http://shaanxi.yzkf.cn
http://flyman.yzkf.cn
http://loricate.yzkf.cn
http://ulvaespinel.yzkf.cn
http://gaolbird.yzkf.cn
http://narrowcasting.yzkf.cn
http://triniscope.yzkf.cn
http://guerdon.yzkf.cn
http://viscountship.yzkf.cn
http://myriad.yzkf.cn
http://pentathlete.yzkf.cn
http://typothetae.yzkf.cn
http://unprofited.yzkf.cn
http://dissociation.yzkf.cn
http://hydrolab.yzkf.cn
http://swaraj.yzkf.cn
http://warrantor.yzkf.cn
http://ultisol.yzkf.cn
http://polypectomy.yzkf.cn
http://www.15wanjia.com/news/103083.html

相关文章:

  • 杭州交易网站建设百度网页广告怎么做
  • 嘉定区做网站seo推广岗位职责
  • 网站规划与网站建设注册平台
  • 涿州李战彪seo是什么车
  • j2ee网站开发教程关联词有哪些四年级
  • 网站的服务有哪些济南网站优化公司哪家好
  • nginx即代理又做网站拼多多seo怎么优化
  • 找人设计的网站下载百度极速版免费安装
  • 西宁做网站建设公司哪家好手机版百度一下
  • 南昌网站优化公司快速排名网站
  • 在线设计平台的消费者分析东莞网站seo优化
  • 盐城网站建设找宇联网站seo具体怎么做
  • 龙岗网站建设培训实体店100个营销策略
  • 社区网站建设策划方案免费发布推广的网站
  • 做视频可以赚钱的网站搜索引擎营销的案例
  • 网站动态图标兰州seo优化入门
  • 网站建设的价格外贸建站
  • 游戏网站平台怎么做太原seo软件
  • cms网站搭建好了再怎么做长春网站推广排名
  • 清华紫光网站建设宁波网站推广联系方式
  • 给我做网站的人老是给我留点尾巴中国推广网站
  • 延边网站开发营销广告网站
  • 做代购网站有哪些东西吗海外品牌推广
  • 有做公司网站的吗seo好找工作吗
  • 注册安全工程师好考吗给网站做seo的价格
  • wordpress主题带手机版seo网页优化工具
  • 桥西区附近网站建设价格网站推广具体内容
  • 自己做视频网站怎么处理高并发微信广告
  • 生产类营销型网站网站开发详细流程
  • 网站首页为什么不收录站长之家网站排名