繁体   English   中英

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

[英]How can I wrap JSON objects in an array using Javascript or Jquery?

如何将所有数据包装在一个数组中? 这是从网站系统自动生成的,而我正在将其输入的应用程序需要将数据存储在数组中。 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 = { 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 } } } var myObject = Object.assign({}, rawJSON.collection); // merging objects aka extend 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) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM