首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何向Javascript数组中添加JSON对象数组

如何向Javascript数组中添加JSON对象数组
EN

Stack Overflow用户
提问于 2015-10-28 11:03:24
回答 3查看 83关注 0票数 2

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

当前代码如下

代码语言: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\"}]}]}';

  console.log(arrData[0]);

从上面的代码中,我得到了如下输出

正如我们所看到的,输出都是文本。如何输出JSON对象的数组,如上面列出的代码中的第一个图像所示。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-10-28 11:14:21

正如您所告诉的,您有一个JSON字符串数组。通过将JSON.parse函数应用于每个项,您可以简单地将其转换为对象数组。

使用Array.prototype.map函数可以很容易地完成:

代码语言:javascript
复制
// 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对象的数组。

工作演示:

代码语言: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);
代码语言:javascript
复制
<div style="color: white;">Wake up Neo...</div>Look at the console

票数 2
EN

Stack Overflow用户

发布于 2015-10-28 11:10:59

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

代码语言:javascript
复制
console.log(JSON.parse(arrData[0]));
票数 1
EN

Stack Overflow用户

发布于 2015-10-28 11:11:38

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

代码语言:javascript
复制
console.log(JSON.parse(arrData[0]));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33389333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档