Arrays 如何将JSON对象数组添加到Javascript数组

Arrays 如何将JSON对象数组添加到Javascript数组,arrays,json,object,Arrays,Json,Object,我有一个JSON对象数组,我想通过编程创建它。所需的输出数组如下所示 var arrData = []; arrData[0] = '{\"GeneralInformation\": [{\"BusinessSummary\": \"Apple Inc. designs, manufactures and markets mobile communication and media devices, personal computers and portable digital music

我有一个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]);

目前的代码如下

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对象数组,如第一幅图所示。

使用方法将JSON字符串解析为JavaScript对象

console.log(JSON.parse(arrData[0]));
将JSON格式的字符串转换为JavaScript对象。 由于
arrData[0]
是一个字符串,为了访问它的参数,您需要使用JSON.parse()


如前所述,您有一个JSON字符串数组。您只需将函数应用于每个项目,即可将其转换为对象数组

使用功能可以轻松完成:

// 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]);
}

唤醒Neo…查看控制台
尝试console.log(arrData);请对您的解决方案添加一些注释,说明其解决问题的原因和方式problem@Odedra,谢谢你的建议,让我的答案更有用。