0

如何将此处的所有数据包装在一个数组中?这是从网站系统自动生成的,我将其输入的应用程序要求数据位于数组中。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
}
}
4

2 回答 2

1

好的,假设您将 API 返回的 JSON 保存在名为rawJson. 这对您来说很容易使用 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]} )
  }
}
于 2013-01-15T06:08:01.663 回答
0

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

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)

于 2017-10-16T09:38:27.990 回答