【问题标题】:How can I wrap JSON objects in an array using Javascript or Jquery?如何使用 Javascript 或 Jquery 将 JSON 对象包装在数组中?
【发布时间】:2012-12-29 04:05:36
【问题描述】:

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

【问题讨论】:

  • @Passerby:问题中没有提到 PHP。
  • 我们需要更多信息来回答问题。你想做什么。我的意思是,如果您只是想将它包装在一个数组中,只需在开头和结尾添加 [] ——您似乎已经拥有有效的 json 对象
  • 对不起。因此,可以通过在 Squarespace 网站平台的页面上附加 ?format=json-pretty 来获得此 JSON 数据。示例:squarespace.com/?format=json-pretty 我需要在我的应用程序中使用 Javascript 或 Jquery 将数据放入数组中,因为该应用程序只识别数组中的数据。
  • 所以数据是作为对象属性给出的,其键是“collection”、“website”、“items”等。你想要一个只包含这些键的值的数组吗?跨度>
  • 问题是它不是数组数据。它是带有键和值的数据。您当然可以创建一个仅包含值的数组,但是您的应用程序如何知道每个值的含义?您必须对每个数组位置的含义进行硬编码?或者你想要一个对象数组,其中每个对象都包含一个键和值?

标签: javascript jquery arrays json object


【解决方案1】:

好的,假设您在名为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]} )
  }
}

【讨论】:

    【解决方案2】:

    您也可以将这两个对象展平并将它们转换为数组,请参见下面的 sn-p。

    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)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-09
      • 2013-07-12
      • 2012-04-15
      • 2011-10-11
      • 1970-01-01
      • 2015-11-22
      • 1970-01-01
      相关资源
      最近更新 更多