typescript json 转 bean
3个文件:
1.UserInfoGetResponse.ts
class UserInfoGetResponse{
private userId: number;
private userName: string;
private userPwd: string;
public getUserId(): number {
return this.userId;
}
public setUserId(value: number) {
this.userId = value;
}
public getUserName(): string {
return this.userName;
}
public setUserName(value: string) {
this.userName = value;
}
public getUserPwd(): string {
return this.userPwd;
}
public setUserPwd(value: string) {
this.userPwd = value;
}
}
export = UserInfoGetResponse;
2.ts2bean.ts
import UserInfoGetResponse = require("./src/js/UserInfoGetResponse");
window.onload = ()=>{
let wokao = this.document.getElementById("wokao");
wokao.onclick = ()=>{
let json = "{\"userId\":777,\"userName\":\"小李飞刀\",\"userPwd\":\"wokao123\"}";
let res1 = new UserInfoGetResponse();
let res2 = new UserInfoGetResponse();
/*
1.这种方式会失去类型信息,导致 res1.getUserName() 报错,显示未定义
*/
res1 = JSON.parse(json);
//console.log(res1.getUserName()); 会报错。
//强制这样写,编译时报错,但是运行时是可以通过的,可见JSON.parse只是转换成了一个纯净的JsonObject,不包含方法
//console.log(res1.userName);
/*
2.这个方法是es6新增的,可以实现不错,不丢失方法(完美方案)
*/
Object.assign(res2, JSON.parse(json));
console.log(res2.getUserName());
//当然 像 res3这样直接写,也是可以的。
//let res3: UserInfoGetResponse = Object.assign(new UserInfoGetResponse(), JSON.parse(json));
/*
是一件好事,仅仅留意下就可。
注意事项,若实体类中不包含一个字段,而json中包含这个字段,那么转换后,虽然不能通过实例对象来获取,但是属性是已经加上了
就是说,JSON.parse 会把 json中的 所有字段都加到对象上,而不管这个对象的类 有没有定义,有定义 就按有定义的来,没定义
就直接增加上,但是 由于没有定义 所以写代码时 无法方法,但是运行时 是可以获取到的,这其实是一件好事。
测试方法:
1. 去除UserInfoGetResponse的 userId字段
2. 然后 console.log(res2.userId()); 会发行IDE会报错因为没有定义,但是运行时 是没有问题的,可以获取到。
通过 JSON.stringify 也会出来,是一件好事,不影响使用。
*/
}
}
export = window.onload
res1 报错截图:

res1 直接写 userName 编译报错,运行正常截图:


res2 运行正常截图,这个省略了。
res2 去除 userId 后, 测试流程截图:




这个问题的来源是某一个人的提问,我也测试了下 不影响使用:https://stackoverflow.com/questions/43019452/typescript-json-to-typescript-object
3.index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
<!-- https://electronjs.org/docs/tutorial/security#csp-meta-tag -->
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" /> <script src="ts2bean.js"></script>
</head>
<body>
<h1>Hello World!</h1>
We are using node <script>document.write(process.versions.node)</script>,
Chrome <script>document.write(process.versions.chrome)</script>,
and Electron <script>document.write(process.versions.electron)</script>. <button id = "wokao">点我</button>
</body>
</html>
typescript json 转 bean的更多相关文章
- JSON,Bean,XML,List,Map
http://blog.csdn.net/superit401/article/details/51728929 JSON-lib这个Java类包用于把bean,map和XML转换成JSON并能够把J ...
- [TypeScript] JSON对象转TypeScript对象范例
[TypeScript] JSON对象转TypeScript对象范例 Playground http://tinyurl.com/nv4x9ak Samples class DataTable { p ...
- Json与bean的相互转换
本文使用json-lib jar包实现Json与bean的相互转换 1.将字符串转为JSON 使用JSONObject.fromObject(str)方法即可将字符串转为JSON对象 使用JSONOb ...
- java中 json和bean list map之间的互相转换总结
JSON 与 对象 .集合 之间的转换 JSON字符串和java对象的互转[json-lib] 在开发过程中,经常需要和别的系统交换数据,数据交换的格式有XML.JSON等,JSON作为一个轻量级 ...
- Json与Bean互转,Timestamp类型的问题
Json与Java Bean互相转换时,Bean中的Timestamp字段是无法直接处理的,需要实现两个转换器. DateJsonValueProcessor的作用是Bean转换为Json时将Time ...
- JSON与Bean互转
转自: 关于json与javaBean之间的转换 废话不多说,直接进入主题,json与javaBean之间的互相转换 javaBean转换成json 直接使用提供的方法可以得到json JSONObj ...
- typescript Json Convert
关键代码 this.data={}; // json string this.dataStr=JSON.stringify(this.data); // json object this.conver ...
- json转bean对象
一下为个人收藏,以便下次使用. 前端传的json格式为: [{"suppliercode":"gylhld_gycqlt3_gycqlt1","pro ...
- jackson json转bean忽略没有的字段 not marked as ignorable
@JsonIgnore注解用来忽略某些字段,可以用在Field或者Getter方法上,用在Setter方法时,和Filed效果一样.这个注解只能用在POJO存在的字段要忽略的情况,不能满足现在需要的情 ...
- gson转换json到bean时重命名
@Expose @SerializedName("0001") public Map<String,ChannelBean> c0001 = new HashMap ...
随机推荐
- uni-app 预览pdf文件
安卓uni-app实现pdf文件预览功能: 1.https://mozilla.github.io/pdf.js/getting_started/#download下载 放在根目录下, 2.新建一个w ...
- 引发C++异常的常见原因(一)从报错地址到错误症状
在进行C++软件开发的过程中,会遇到很多问题,网上差不到,或者查到了也没什么信息可以用,所以这里想到了就将一些常见的问题放在一起,归纳整理一下. 本文主要的内容来源于CSDN的大佬文章:https:/ ...
- P1439-DP【绿】
轻敌了啊...题目一共只有几句话但我却忽略了一个重大信息... 总之我显示写出了时空复杂度都是n^2级别的朴素递推算法,这没什么,基本功而已,然后50分 我试了试滚动数组,把空间复杂度降到了n级别,但 ...
- java基础-方法method-day05
1. 语法 [修饰符] 返回值类型 方法名(形参列表){ 逻辑主体 } public class TestMethod01{ public static int add(int num1, int n ...
- MySQL 覆盖索引详解
本文转载自:MySQL 覆盖索引详解,作者 Sevn 1. 什么是索引? 索引(在 MySQL 中也叫"键key")是存储引擎快速找到记录的一种数据结构,通俗来说类似书本的目录. ...
- Verilog仿真实践
Verilog必须掌握 逻辑仿真工具(VCS)和逻辑综合工具(DC) AndOR module AndOr( output X,Y, input A,B,C ); // A B进行按位与运算 assi ...
- 神经网络优化篇:详解调试处理(Tuning process)
调试处理 关于训练深度最难的事情之一是要处理的参数的数量,从学习速率\(a\)到Momentum(动量梯度下降法)的参数\(\beta\).如果使用Momentum或Adam优化算法的参数,\(\be ...
- PG数据库存储验证
PG数据库存储验证 背景 最近学习了SQLServer数据库的varchar和nvarchar的存储 想到PG数据库其实没让选择字符集,也没有nvarchar 所以想学习一下nvarchar的使用情况 ...
- [转帖]MySQL的版本情况
Introducing MySQL Innovation and Long-Term Support (LTS) versions (oracle.com) Introducing MySQL Inn ...
- 银河麒麟安装多版本gcc的方式方法
银河麒麟安装多版本gcc的方式方法 背景 最近想升级一下gcc 但是发现自己编译的话非常麻烦 记得之前CentOS7的时候有一个scl的处理 发现CentOS8 已经没有scl的仓库了 简单验证了一下 ...