将json数组转换为typescript中的对象数组

时间:2018-04-09 12:56:48

标签: arrays json typescript type-conversion angular5

我有json类型的数组我希望它在对象类型数组中转换

JSON数组

[  
0:{code: "00125", scheme: "0001", plotNumber: "125", propType: "001", plotType: "001", }
1:{code: "190", scheme: "0001", plotNumber: "NA 190", propType: "001", plotType: "001"}
2:{code: "210", scheme: "0001", plotNumber: "NA210", propType: "001", plotType: "001"}
]

想要像这样转换为对象数组

 [
{code: "00125", scheme: "0001", plotNumber: "125", propType: "001", plotType: "001" },
    {code: "190", scheme: "0001", plotNumber: "NA 190", propType: "001", plotType: "001"},
    {code: "210", scheme: "0001", plotNumber: "NA210", propType: "001", plotType: "001"}
    ]

1 个答案:

答案 0 :(得分:1)

此数组

[  
   0:{code: "00125", scheme: "0001", plotNumber: "125", propType: "001", plotType: "001", }
   1:{code: "190", scheme: "0001", plotNumber: "NA 190", propType: "001", plotType: "001"}
   2:{code: "210", scheme: "0001", plotNumber: "NA210", propType: "001", plotType: "001"}
]

不是JSON。所以你无法转换它。如果它是JSON,它将如下所示:

[  
   {"code": "00125", "scheme": "0001", "plotNumber": "125", "propType": "001", "plotType": "001" },
   {"code": "190", "scheme": "0001", "plotNumber": "NA 190", "propType": "001", "plotType": "001"}, 
   ...
]

你从哪里得到它,因为你认为它应该是一个JSON?或者这仅仅是您手动编写的一个示例?

如果您有一个真正的JSON字符串,并且想要将其转换回对象,请执行以下操作:

let myObject = JSON.parse(jsonString);

反过来说:

let jsonString = JSON.stringify(myObject);