我想要以编程方式创建一个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对象的数组,如上面列出的代码中的第一个图像所示。
答案 0 :(得分:2)
正如您所说,您有一组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
答案 1 :(得分:1)
使用JSON.parse
方法将JSON字符串解析为JavaScript对象。
console.log(JSON.parse(arrData[0]));
答案 2 :(得分:1)
JSON.parse()将JSON格式的字符串转换为JavaScript对象。
由于arrData[0]
是一个字符串,为了访问其参数,您需要使用JSON.parse()
console.log(JSON.parse(arrData[0]));