Javascript JSON对象数组

Javascript JSON对象数组,javascript,json,object,Javascript,Json,Object,早上好 我是JSON新手,正在尝试使用JSON而不是一些二维数组重新实现页面 我希望实现的是获得一组对象。对象将如下所示: { // Restaurant "location" : "123 Road Dr", "city_state" : "MyCity ST", "phone" : "555-555-5555", "distance" : "0" } function Restaurant(location, city_state, phone, distance) {

早上好

我是JSON新手,正在尝试使用JSON而不是一些二维数组重新实现页面

我希望实现的是获得一组对象。对象将如下所示:

{ // Restaurant
  "location" : "123 Road Dr",
  "city_state" : "MyCity ST",
  "phone" : "555-555-5555",
  "distance" : "0"
}
function Restaurant(location, city_state, phone, distance) {
  this.location = location;
  this.city_state = city_state;
  this.phone = phone;
  // here you can add some logic for the distance field, if you like:
  this.distance = distance;
}

// create an array restaurants
var restaurants = [];
// add objects to the array
restaurants.push(new Restaurant("123 Road Dr", "MyCity ST", "555-555-5555", 0));
restaurants.push(new Restaurant("123 Road Dr", "MyCity ST", "555-555-5555", 0));
restaurants.push(new Restaurant("123 Road Dr", "MyCity ST", "555-555-5555", 0));
我想创建这些餐厅对象的数组,并用一些逻辑填充距离字段,然后根据距离字段对数组进行排序

我对JSON很陌生。我可以创建一个JSON对象数组吗?或者JSON是否还有其他东西可以实现这个目标

非常感谢你的帮助


凯文:当然可以。它看起来像这样:

{ "restaurants": [ 
    { "location" : "123 Road Dr", "city_state" : "MyCity ST", "phone" : "555-555-5555", "distance" : "0" } , 
    { "location" : "456 Fake St", "city_state" : "MyCity ST", "phone" : "555-123-1212", "distance" : "0" } 
] }
当然,“restaurants”的外部字段名不是必需的,但如果您在传输的数据中包含其他信息,则可能会有所帮助

//可以将餐厅声明为餐厅对象数组
[
    { "location" : "123 Road Dr", "city_state" : "MyCity ST", "phone" : "555-555-5555", "distance" : "0" },
    { "location" : "123 Road Dr", "city_state" : "MyCity ST", "phone" : "555-555-5555", "distance" : "0" },
    { "location" : "123 Road Dr", "city_state" : "MyCity ST", "phone" : "555-555-5555", "distance" : "0" }
]
餐厅= [ { “位置”:“123路Dr”, “城市州”:“我的城市街”, “电话”:“555-555-5555”, “距离”:“1” }, { “位置”:“456大道Crt”, “城邦”:“MyTown AL”, “电话”:“555-867-5309”, “距离”:“0” } ]; //然后使用for循环对其进行操作 对于(变量i=0;i<0.length;i++){ 餐厅[i]。距离=餐厅[i]。距离;//或其他逻辑。 } //最后,您可以使用这样的匿名函数对它们进行排序 排序(函数(a,b){返回a.distance-b.distance;});
首先,这根本不是JSON,您只是在使用Javascript对象。JSON是一种表示对象的文本格式,没有“JSON对象”这样的东西

您可以为对象创建构造函数,如下所示:

{ // Restaurant
  "location" : "123 Road Dr",
  "city_state" : "MyCity ST",
  "phone" : "555-555-5555",
  "distance" : "0"
}
function Restaurant(location, city_state, phone, distance) {
  this.location = location;
  this.city_state = city_state;
  this.phone = phone;
  // here you can add some logic for the distance field, if you like:
  this.distance = distance;
}

// create an array restaurants
var restaurants = [];
// add objects to the array
restaurants.push(new Restaurant("123 Road Dr", "MyCity ST", "555-555-5555", 0));
restaurants.push(new Restaurant("123 Road Dr", "MyCity ST", "555-555-5555", 0));
restaurants.push(new Restaurant("123 Road Dr", "MyCity ST", "555-555-5555", 0));

是的,你可以创建一个对象数组。从技术上讲,二维数组是可以的。事实上,几乎所有没有特殊功能的对象(例如DOM对象和类似
new Date()
new Image()
)都可以被称为JSON。但是,通过使用具有命名值的对象,您肯定采取了更好的方法。最初的问题是关于如何操作
距离
字段,然后根据该值进行排序。您能告诉我如何操作吗?