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的更多相关文章

  1. JSON,Bean,XML,List,Map

    http://blog.csdn.net/superit401/article/details/51728929 JSON-lib这个Java类包用于把bean,map和XML转换成JSON并能够把J ...

  2. [TypeScript] JSON对象转TypeScript对象范例

    [TypeScript] JSON对象转TypeScript对象范例 Playground http://tinyurl.com/nv4x9ak Samples class DataTable { p ...

  3. Json与bean的相互转换

    本文使用json-lib jar包实现Json与bean的相互转换 1.将字符串转为JSON 使用JSONObject.fromObject(str)方法即可将字符串转为JSON对象 使用JSONOb ...

  4. java中 json和bean list map之间的互相转换总结

    JSON 与 对象 .集合 之间的转换 JSON字符串和java对象的互转[json-lib]   在开发过程中,经常需要和别的系统交换数据,数据交换的格式有XML.JSON等,JSON作为一个轻量级 ...

  5. Json与Bean互转,Timestamp类型的问题

    Json与Java Bean互相转换时,Bean中的Timestamp字段是无法直接处理的,需要实现两个转换器. DateJsonValueProcessor的作用是Bean转换为Json时将Time ...

  6. JSON与Bean互转

    转自: 关于json与javaBean之间的转换 废话不多说,直接进入主题,json与javaBean之间的互相转换 javaBean转换成json 直接使用提供的方法可以得到json JSONObj ...

  7. typescript Json Convert

    关键代码 this.data={}; // json string this.dataStr=JSON.stringify(this.data); // json object this.conver ...

  8. json转bean对象

    一下为个人收藏,以便下次使用. 前端传的json格式为: [{"suppliercode":"gylhld_gycqlt3_gycqlt1","pro ...

  9. jackson json转bean忽略没有的字段 not marked as ignorable

    @JsonIgnore注解用来忽略某些字段,可以用在Field或者Getter方法上,用在Setter方法时,和Filed效果一样.这个注解只能用在POJO存在的字段要忽略的情况,不能满足现在需要的情 ...

  10. gson转换json到bean时重命名

    @Expose   @SerializedName("0001") public Map<String,ChannelBean> c0001 = new HashMap ...

随机推荐

  1. Shell 脚本编程学习

    本文为博主原创,转载请注明出处: 目录: 1. shell 变量 2. 运算符 3. if 语句 4.for 循环 5.while 语句 6. case 语法 7.跳出循环:continue 与 br ...

  2. linux tcpdump 使用小结(一)

    转载请注明出处: 很多情况在服务联调和定位过程中,需要在服务器上进行抓包进行问题定位.在Linux环境上使用tcpdump命令进行抓包: 1.tcpdump -s 0 -l -i any -w - p ...

  3. Nacos源码 (7) Nacos与Spring

    SpringCloud工程可以使用Nacos作为注册中心和配置中心,配置和使用非常简单,本文将简单介绍使用方式,并分析其实现方式. SpringCloud工程集成Nacos SpringCloud工程 ...

  4. printf 函数格式控制

    Printf()介绍 printf()是C语言标准库函数,用于将格式化后的字符串输出到标准输出.标准输出,即标准输出文件,对应终端的屏幕.printf()申明于头文件stdio.h. 函数原型: in ...

  5. Laravel - Eloquent 模型查询

    Laravel 的 Eloquent ORM 提供了漂亮.简洁的 ActiveRecord 实现来和数据库进行交互.每个数据库表都有一个对应的「模型」可用来跟数据表进行交互.你可以通过模型查找数据表内 ...

  6. Oracle表结构&数据类型&约束

    1.Oracle 表结构 1.1.创建表名的规范 推荐以"t_xxx" 不能含有"+- "等非法字符,eg:sql create table "t-1 ...

  7. ONVIF网络摄像头(IPC)客户端开发—最简RTSP客户端实现

    前言: 网上对于RTSP协议客户端的表述和实现非常不清晰,在实际使用中,FFMPEG和live555这些软件已经实现了RTSP客户端和服务端的所有功能,但是它们并没有将RTSP协议独立出来,通过看li ...

  8. 第二章 VB.NET 绘图基础

    GDI+( Graphics Device Interface Plus)是 Windows操作系统用来执行绘画及其他相关图形操作的一套子系统,是由. Net Framework中的System.Dr ...

  9. [转帖]【TiDB】快速起步

    1. 存储引擎的的功能 提供数据存储接口并持久化存储数据 2. LSM-tree 的特性 LSM-tree 结构本质上是一个用空间置换写入延迟,用顺序写入替换随机写入的数据结构 3. 数据库技术的发展 ...

  10. [转帖]Nginx-https证书认证详解

    https://developer.aliyun.com/article/885650?spm=a2c6h.24874632.expert-profile.306.7c46cfe9h5DxWK 简介: ...