如何使用Javascript或Jquery将JSON对象包装到数组中?

如何使用Javascript或Jquery将JSON对象包装到数组中?,javascript,jquery,arrays,json,object,Javascript,Jquery,Arrays,Json,Object,如何将此处的所有数据包装成一个数组?这是从网站系统自动生成的,我将其输入的应用程序要求数据位于数组中。JSON中有多组数据,这只是5或6个数据中的2个 collection: { id: "5096f729e4b02d37bef658f2", enabled: true, starred: false, type: 10, ordering: 3, title: "About", navigationTitle: "About", urlId: "about", itemCount: 0, up

如何将此处的所有数据包装成一个数组?这是从网站系统自动生成的,我将其输入的应用程序要求数据位于数组中。JSON中有多组数据,这只是5或6个数据中的2个

collection: {
id: "5096f729e4b02d37bef658f2",
enabled: true,
starred: false,
type: 10,
ordering: 3,
title: "About",
navigationTitle: "About",
urlId: "about",
itemCount: 0,
updatedOn: 1347025745523,
publicCommentCount: 0,
folder: false,
dropdown: false,
tags: [ ],
categories: [ ],
homepage: true,
typeName: "page",
synchronizing: false,
typeLabel: "page",
fullUrl: "/"
},

websiteSettings: {
id: "5096f728e4b02d37bef658e0",
websiteId: "5096f728e4b02d37bef658df",
type: "Business",
subject: "Personal",
country: "US",
state: "NY",
markdownMode: false,
simpleLikingEnabled: true,
commerceEnabled: false,
defaultPostFormat: "%y/%m/%d/%t",
commentLikesAllowed: true,
commentAnonAllowed: true,
commentThreaded: true,
commentApprovalRequired: false,
commentAvatarsOn: true,
commentSortType: 2,
commentFlagThreshold: 0,
commentFlagsAllowed: true,
commentEnableByDefault: true,
commentDisableAfterDaysDefault: 0,
disqusShortname: "username",
homepageTitleFormat: "%s - This is a test",
collectionTitleFormat: "%c — %s - This is a test",
itemTitleFormat: "%i — %s - This is a test",
commentsEnabled: true,
allowSquarespacePromotion: false,
storeSettings: {
storeTitle: "Test",
returnPolicy: null,
termsOfService: null,
privacyPolicy: null,
stockLevelAlertLimit: 5,
useLightCart: false,
stripeConnected: false,
storeState: 3
}
}

好的,假设您在一个名为
rawJson
的变量中从API返回了JSON。这对于使用jquery非常容易,例如使用
getJSON
。现在,您可以通过以下代码实现所需:

var rawJson = // get this yourself, pretend I'm inserting the JSON literal from the url you linked to above in your comments
arrayYouWant = [];

for (category in rawJson) {
  for (key in rawJson[category]) {
    arrayYouWant.push( {key: rawJson[category][key]} )
  }
}

您还可以展平这两个对象并将其转换为数组,请参见下面的代码段

var rawJSON={
收藏:{
id:“5096f729e4b02d37bef658f2”,
启用:对,
主演:错,
类型:10,
订购:3,
标题:“关于”,
导航标题:“关于”,
urlId:“关于”,
itemCount:0,
更新日期:1347025745523,
publicCommentCount:0,
文件夹:false,
下拉列表:false,
标签:[],
类别:[],
主页:对,
字体名称:“第页”,
同步:错误,
类型标签:“页面”,
完整URL:“/”
},
网站设置:{
id:“5096f728e4b02d37bef658e0”,
网站ID:“5096f728e4b02d37bef658df”,
类型:“业务”,
主题:“个人”,
国家:“美国”,
国家:“纽约”,
markdownMode:false,
simpleLikingEnabled:true,
已启用商业:false,
defaultPostFormat:“%y/%m/%d/%t”,
允许的评论:正确,
评论:没错,
对,,
commentApprovalRequired:false,
是的,
评论类型:2,
阈值:0,
评论:是的,
commentEnableByDefault:true,
CommentDisableAfterdays错误:0,
名称:“用户名”,
homepageTitleFormat:“%s-这是一个测试”,
collectionTitleFormat:“%c-%s-这是一个测试”,
itemTitleFormat:“%i-%s-这是一个测试”,
评论:是的,
AllowSquarespace升级:false,
商店设置:{
店名:“测试”,
returnPolicy:null,
termsOfService:null,
privacyPolicy:null,
库存水平警报限值:5,
useLightCart:false,
StripeConnect:false,
存储状态:3
}
}
}
var myObject=Object.assign({},rawJSON.collection);//合并对象或扩展
myObject=Object.assign(myObject,rawJSON.websiteSettings);
//{a:1,b:2}=>[[a',1],[b',2]]
var objAsAnArray=Object.keys(myObject.map)((k)=>[k,JSON.stringify(myObject[k]))

console.log(objAsAnArray)
@Passerby:问题中没有提到PHP。我们需要更多信息来回答问题。你想干什么。我的意思是,如果你真的想把它包装成一个数组,只需在开头和结尾添加
[
]
——你似乎已经拥有了有效的json ObjectsOry。所以这个JSON数据可以通过在Squarespace网站平台的页面上添加?format=JSON pretty来获得。示例:我需要在我的应用程序中使用Javascript或Jquery将数据放入数组中,因为应用程序只识别数组中的数据。因此,数据作为对象属性提供,其键为“集合”、“网站”、“项目”等等。你想要一个仅仅由这些键的值组成的数组吗?问题是它不是数组数据。它是包含键和值的数据。当然,您可以创建一个仅由值组成的数组,但是您的应用程序如何知道每个值的含义呢?您必须对每个数组位置的含义进行硬编码?或者您想要一个对象数组,其中每个对象都包含一个键和值?