我使用Sequelize 2.0.2和mysql在nodeJs中创建了rest full web服务 我是通过在Sequelize和Sequelize文字中使用eager-loading概念来完成的 所以我想要特定格式的json数据 我给了一些示例代码 我的示例代码:
var results=JSON.parse(JSON.stringify(result));
var data=[];
var states;
var districts;
var cities;
var locations;
var sub_locations;
var properties;
var builders;
console.log(JSON.stringify(result));
for (var country in results) {
states=results[country].hp_states;
for(var state in states){
districts=states[state].hp_districts;
data.push({state_id: states[state].state_id, state_name: states[state].state_name});
for(var district in districts) {
cities = districts[district].hp_cities;
data[state]["district_id"]=districts[district].district_id;
data[state]["district_name"]=districts[district].district_name;
for(var city in cities) {
locations = cities[city].hp_locations;
data[district]["city_id"]=cities[city].city_id;
data[district]["city_name"]=cities[city].city_name;
for(var location in locations) {
sub_locations=locations[location].hp_sub_locations;
data[city]["location_id"]=locations[location].location_id;
data[city]["location_name"]=locations[location].location_name;
for(var sublocation in sub_locations){
properties=sub_locations[sublocation].hp_property;
/*data[location]["sub_location_id"]=sub_locations[sublocation].sub_location_id;
data[location]["sub_location_name"]=sub_locations[sublocation].sub_location_name;
console.log(JSON.stringify(properties)+"properties...");
*/
}
}
}
}
}
}
我从数据库获得此结果:
我想要这种格式:
Json格式我得到了:
[{"country_id":1,"country_name":"India","hp_states":[{"state_id":1,"state_name":"Tamil Nadu","hp_districts":[{"district_id":3,"district_name":"Erode","hp_cities":[{"city_id":3,"city_name":"Erode","hp_locations":[{"location_id":141,"location_name":"Chellamma Road","hp_sub_locations":[{"sub_location_id":7,"sub_location_name":"Ganga Road","hp_property":{"property_id":3,"property_name":"golden","hp_builder":{"builders_id":21,"builders_name":"golden homes"}}}]}]}]}]},{"state_id":2,"state_name":"Karnataka","hp_districts":[{"district_id":5,"district_name":"Bangalore","hp_cities":[{"city_id":5,"city_name":"Bangalore","hp_locations":[{"location_id":15,"location_name":"Hebbal","hp_sub_locations":[{"sub_location_id":5,"sub_location_name":"Hebbal Police Station","hp_property":{"property_id":1,"property_name":"Godrej Woodsman Estate","hp_builder":{"builders_id":1,"builders_name":"Godrej"}}},{"sub_location_id":6,"sub_location_name":"Hebbal Ring road","hp_property":{"property_id":2,"property_name":"Skyline","hp_builder":{"builders_id":3,"builders_name":"Skyline"}}}]},{"location_id":14,"location_name":"Marathahalli","hp_sub_locations":[{"sub_location_id":4,"sub_location_name":"Kundanalli Gate","hp_property":{"property_id":4,"property_name":"golden","hp_builder":{"builders_id":21,"builders_name":"golden homes"}}}]}]}]}]}]}]
答案 0 :(得分:1)
在这里,我将位置更改为&#39; loc&#39;,因为我在浏览器中测试了它,并且在浏览器中预定义了位置,如果您使用的是节点,则可能使用位置。< / p>
var results=JSON.parse(JSON.stringify(result));
var data=[];
var states;
var districts;
var cities;
var locations;
var sub_locations;
var properties;
var builders;
for (var country in results) {
states=results[country].hp_states;
for(var state in states){
districts=states[state].hp_districts;
for(var district in districts) {
cities = districts[district].hp_cities;
for(var city in cities) {
locations = cities[city].hp_locations;
for(var loc in locations) {
//location is a browser predefined variable, you can't call your var as location.
sub_locations=locations[loc].hp_sub_locations;
for(var sublocation in sub_locations){
properties=sub_locations[sublocation].hp_property;
data.push({
state_name: states[state].state_name,
district_name: districts[district].district_name,
location_name: locations[loc].location_name,
sub_location_id: sub_locations[sublocation].sub_location_id,
property_id: properties.property_id,
property_name: properties.property_name,
builders_id: properties.hp_builder.builders_id,
builders_name: properties.hp_builder.builders_name
});
}
}
}
}
}
}
console.log(data);//data contains 4 items.