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打包到安卓步骤
1.打包到安卓 https://www.bilibili.com/video/BV1BJ411W7pX?p=56 2.打包到IOS https://www.jianshu.com/p/ef6e6e01 ...
- uni-app editor富文本编辑器
https://blog.csdn.net/xudejun/article/details/91508189
- paddlespeech on centos7
概述 paddlespeech是百度飞桨平台的开源工具包,主要用于语音和音频的分析处理,其中包含多个可选模型,提供语音识别.语音合成.说话人验证.关键词识别.音频分类和语音翻译等功能. paddles ...
- Blazor模式讲解
Blazor的三种模式 Blazor Server: Blazor Server在 ASP.NET Core 应用中支持在服务器上托管 Razor 组件. 可通过 SignalR 连接处理 UI 更新 ...
- Angular系列教程之路由守卫
.markdown-body { line-height: 1.75; font-weight: 400; font-size: 16px; overflow-x: hidden; color: rg ...
- JS - console多个值
Promise.all([p1, p2]).then( (res) => { let [p1, p2] = res; console.l ...
- Laravel - 改为国内镜像
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/ (阿里云) 或 composer ...
- JMS微服务开发示例(六)安全退出进程
默认情况下,如果在linux,需要关闭微服务进程,请务必使用 kill -15 进程id 命令,其他命令可能会直接关闭进程,造成数据丢失. 例如,有个后台任务,执行了一半,这时候进程突然关闭了,会形成 ...
- Go-发送邮件
1. 邮件 - mail From -- 发送者(这封邮件由谁进行发送的,一般都是该邮件的作者) To -- 邮件的接收者(发送邮件的人希望谁能收到邮件) Subject -- 邮件的主题(类似文章的 ...
- [转帖]PostgreSQL 参数优化设置 32GB内存(推荐) 内存参数 检查点 日志参数 自动初始化参数shell脚本
1.修改参数列表 (1)执行计划 enable_nestloop = off #默认为on enable_seqscan = off #默认为on enable_indexscan = on enab ...