2013-01-15 70 views
0

如何將所有數據包裝在數組中?這是從一個網站系統自動生成的,我將它提供給它的應用程序需要數據在一個數組中。還有多組的JSON數據,這僅僅是2約5或6如何使用Javascript或Jquery在數組中包裝JSON對象?

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 
} 
} 
+1

@Passerby:PHP中的問題無處提及。 – PleaseStand

+0

我們需要更多信息來回答問題。你想做什麼。我的意思是,如果你真的只想把它包裝在一個數組中,只需在開頭和結尾添加'['和']' - 你已經有了有效的json對象 – Jonah

+0

抱歉。所以這個JSON數據可以通過在Squarespace網站平臺的頁面上添加?format = json-pretty來獲得。例如:http://squarespace.com/?format=json-pretty我需要在我的應用程序中使用Javascript或Jquery將數據放入數組中,因爲應用程序只識別數組中的數據。 – jasonbarone

回答

1

好吧,假設你有JSON從一個叫rawJson變量的API返回。這很容易讓你用jQuery做,例如getJSON。現在,您可以acheive要使用此代碼是什麼:

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]}) 
    } 
} 
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)

相關問題