我有一个希望以编程方式创建的JSON对象数组。所需的输出数组如下

当前代码如下
var arrData = [];
arrData[0] = '{\"GeneralInformation\": [{\"BusinessSummary\": \"Apple Inc. designs, manufactures and markets mobile communication and media devices, personal computers and portable digital music players and sells a variety of related software, services, peripherals, networking solutions and third-party digital content and applications. The Company\'s; products and services include iPhone, iPad, Mac, iPod, Apple TV, a portfolio of consumer and professional software applications, the iOS and OS X operating systems, iCloud and a variety of accessory, service and support offerings. The Company offers a range of mobile communication and media devices, personal computing products and portable digital music players, as well as a variety of related software, services, peripherals, networking solutions and third-party hardware and software products. The Company\'s primary products include iPhone, iPad, Mac, iPod, iTunes, Mac App Store, iCloud, Operating System Software, Application Software and Other Application Software.\"},{\"Websites\": [{\"Homepage\": \"http:www.apple.com\"}]}]}';
console.log(arrData[0]);从上面的代码中,我得到了如下输出

正如我们所看到的,输出都是文本。如何输出JSON对象的数组,如上面列出的代码中的第一个图像所示。
发布于 2015-10-28 11:14:21
正如您所告诉的,您有一个JSON字符串数组。通过将JSON.parse函数应用于每个项,您可以简单地将其转换为对象数组。
使用Array.prototype.map函数可以很容易地完成:
// generates a new array by applying JSON.parse to every item
arrData = arrData.map(JSON.parse);
// just for better understanding. "map" is almost the same as:
for (var i = 0; i < arrData.length; i++)
{
arrData[i] = JSON.parse(arrData[i]);
}之后,arrData将成为JavaScript对象的数组。
工作演示:
var arrData = [];
arrData[0] = '{\"GeneralInformation\": [{\"BusinessSummary\": \"Apple Inc. designs, manufactures and markets mobile communication and media devices, personal computers and portable digital music players and sells a variety of related software, services, peripherals, networking solutions and third-party digital content and applications. The Company\'s; products and services include iPhone, iPad, Mac, iPod, Apple TV, a portfolio of consumer and professional software applications, the iOS and OS X operating systems, iCloud and a variety of accessory, service and support offerings. The Company offers a range of mobile communication and media devices, personal computing products and portable digital music players, as well as a variety of related software, services, peripherals, networking solutions and third-party hardware and software products. The Company\'s primary products include iPhone, iPad, Mac, iPod, iTunes, Mac App Store, iCloud, Operating System Software, Application Software and Other Application Software.\"},{\"Websites\": [{\"Homepage\": \"http:www.apple.com\"}]}]}';
arrData[1] = '{\"GeneralInformation\": [{\"BusinessSummary\": \"Apple Inc. designs, manufactures and markets mobile communication and media devices, personal computers and portable digital music players and sells a variety of related software, services, peripherals, networking solutions and third-party digital content and applications. The Company\'s; products and services include iPhone, iPad, Mac, iPod, Apple TV, a portfolio of consumer and professional software applications, the iOS and OS X operating systems, iCloud and a variety of accessory, service and support offerings. The Company offers a range of mobile communication and media devices, personal computing products and portable digital music players, as well as a variety of related software, services, peripherals, networking solutions and third-party hardware and software products. The Company\'s primary products include iPhone, iPad, Mac, iPod, iTunes, Mac App Store, iCloud, Operating System Software, Application Software and Other Application Software.\"},{\"Websites\": [{\"Homepage\": \"http:www.apple.com\"}]}]}';
arrData = arrData.map(JSON.parse);
console.log(arrData);<div style="color: white;">Wake up Neo...</div>Look at the console
发布于 2015-10-28 11:10:59
使用JSON.parse方法将JSON字符串解析为JavaScript对象。
console.log(JSON.parse(arrData[0]));发布于 2015-10-28 11:11:38
JSON.parse()将JSON格式的字符串转换为JavaScript对象。由于arrData[0]是一个字符串,为了访问它的参数,您需要使用JSON.parse()
console.log(JSON.parse(arrData[0]));https://stackoverflow.com/questions/33389333
复制相似问题