json字符串为:
{"as":{"1":"0"},"cmd":"xxx","from":"yyy"}
如何解析成对象Test:
class Test {
cmd: string = ""
from: string = ""
as: Map<String, Object> = new Map()
}
let a: Test = JSON.parse(json);
这样不行么,json解析成map对象是如何解析的?
json字符串为:
{"as":{"1":"0"},"cmd":"xxx","from":"yyy"}
如何解析成对象Test:
class Test {
cmd: string = ""
from: string = ""
as: Map<String, Object> = new Map()
}
let a: Test = JSON.parse(json);
这样不行么,json解析成map对象是如何解析的?
1 回答563 阅读✓ 已解决
1.3k 阅读
534 阅读
JSON转换Map也可参考示例:
Map和Record类型介绍,详情可以参考文档:https://geek-docs.com/typescript/typescript-questions/94\_typescript\_differences\_between\_and\_when\_to\_use\_map\_vs\_record.html\#:\~:text=%E5%9C%A8%20TypeScript%20%E4%B8%AD%EF%BC%8CMap%20%E5%92%8C%20Record%20%E6%98%AF%E4%B8%A4%E7%A7%8D%E5%AD%98%E5%82%A8%E9%94%AE%E5%80%BC%E5%AF%B9%E7%9A%84%E6%96%B9%E5%BC%8F%E3%80%82,Map%20%E5%85%81%E8%AE%B8%E6%88%91%E4%BB%AC%E5%B0%86%E4%BB%BB%E6%84%8F%E7%B1%BB%E5%9E%8B%E7%9A%84%E9%94%AE%E4%B8%8E%E5%80%BC%E7%9B%B8%E5%85%B3%E8%81%94%EF%BC%8C%E5%B9%B6%E6%8F%90%E4%BE%9B%E4%BA%86%E5%A4%9A%E7%A7%8D%E6%96%B9%E6%B3%95%E6%9D%A5%E6%93%8D%E4%BD%9C%20Map%E3%80%82%20%E8%80%8C%20Record%20%E5%88%99%E7%94%A8%E4%BA%8E%E8%A1%A8%E7%A4%BA%E5%85%B7%E6%9C%89%E7%89%B9%E5%AE%9A%E9%94%AE%E5%92%8C%E5%80%BC%E7%B1%BB%E5%9E%8B%E7%9A%84%E5%AF%B9%E8%B1%A1%EF%BC%8C%E5%B9%B6%E9%80%82%E7%94%A8%E4%BA%8E%E4%B8%8D%E9%9C%80%E8%A6%81%E5%AF%B9%E9%94%AE%E6%89%A7%E8%A1%8C%E7%89%B9%E5%AE%9A%E6%93%8D%E4%BD%9C%E7%9A%84%E6%83%85%E5%86%B5%E3%80%82