Javascript JSON 对象数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6791468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Array of JSON Objects
提问by Bring Coffee Bring Beer
Good morning,
早上好,
I am new to JSON and am trying to re-implement a page using JSON instead of some 2-dimensional arrays.
我是 JSON 的新手,正在尝试使用 JSON 而不是一些二维数组来重新实现页面。
What I am hoping to accomplish is get an array of objects. The objects would look like this:
我希望完成的是获取一组对象。对象看起来像这样:
{ // Restaurant
"location" : "123 Road Dr",
"city_state" : "MyCity ST",
"phone" : "555-555-5555",
"distance" : "0"
}
I want to create an array of these restaurant objects and populate the distance field with some logic, then sort the array based on the distance field.
我想创建一个这些餐厅对象的数组,并用一些逻辑填充距离字段,然后根据距离字段对数组进行排序。
I am VERY new to JSON. Can I create an array of JSON objects or is there something else with JSON that accomplishes this goal?
我对 JSON 很陌生。我可以创建一个 JSON 对象数组,还是 JSON 有其他东西可以实现这个目标?
Thanks very much for your help.
非常感谢您的帮助。
Kevin
凯文
回答by nwellcome
// You can declare restaurants as an array of restaurant objects
restaurants =
[
{
"location" : "123 Road Dr",
"city_state" : "MyCity ST",
"phone" : "555-555-5555",
"distance" : "1"
},
{
"location" : "456 Avenue Crt",
"city_state" : "MyTown AL",
"phone" : "555-867-5309",
"distance" : "0"
}
];
// Then operate on them with a for loop as such
for (var i = 0; i< restaurants.length; i++) {
restaurants[i].distance = restaurants[i].distance; // Or some other logic.
}
// Finally you can sort them using an anonymous function like this
restaurants.sort(function(a,b) { return a.distance - b.distance; });
回答by Guffa
First of all, this is not JSON at all, you are just using Javascript objects. JSON is a text format for representing objects, there is no such thing as a "JSON object".
首先,这根本不是 JSON,您只是在使用 Javascript 对象。JSON 是一种用于表示对象的文本格式,没有“JSON 对象”这样的东西。
You can create a constructor for your objects like this:
您可以为您的对象创建一个构造函数,如下所示:
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));
回答by Mike
Sure you can. It would look something like this:
你当然可以。它看起来像这样:
{ "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" }
] }
The outer field name of "restaurants" is not necessary of course, but it may help if you are including other information in the data you're transferring.
“restaurants”的外部字段名称当然不是必需的,但如果您在传输的数据中包含其他信息,它可能会有所帮助。
回答by Joyce Babu
[
{ "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" }
]