Create multidimentional JSon array from multiple tables
I am trying to create a json array with a structure like this:
var cars = [
{name: 'Honda', models: [
{name: 'Accord', features: ['2dr', '4dr']},
{name: 'CRV', features: ['2dr', 'Hatchback']},
{name: 'Pilot', features: ['base', 'superDuper']}
]},
{name: 'Toyota', models: [
{name: 'Prius', features: ['green', 'superGreen']},
{name: 'Camry', features: ['sporty', 'square']},
{name: 'Corolla', features: ['cheap', 'superFly']}
]}
];
From 3 tables (carmakes
, carmake_models
and carmake_options
) in my MySQL database.
What is the best way to go about it?
The tables are structured as follows:
carmakes: ID, CarName
carmake_models: ModelParen开发者_如何学JAVAt, ModelName carmake_options: OptionParent, OptionName
I don't have enough information to answer your question. However, you might do better to create JSON more like this:
var cars = {
'Honda': {
'Accord': ['2dr', '4dr'],
'CRV': ['2dr', 'Hatchback'],
'Pilot': ['base', 'superDuper']
},
'Toyota': {
'Prius': ['green', 'superGreen'],
'Camry': ['sporty', 'square'],
'Corolla': ['cheap', 'superFly']
}
};
精彩评论