有人可以帮我提供一些有关如何从具有对象数组的对象访问数据的代码吗?这是我的数组:
{
"data": [{
"order_id": "336566",
"customer_name": "joel",
"customer_surname": "kabeya",
"total_items": "0",
"completed_items": "0",
"percent_complete": 1,
"datetime_received": "2019-01-21 15:00:27",
"delivery_date": "2019-01-24",
"delivery_from": "10:30",
"delivery_to": "12:00",
"completed": "1",
"shopper": "joel"
}, {
"order_id": "335719",
"customer_name": "joel",
"customer_surname": "kabeya",
"total_items": "0",
"completed_items": "0",
"percent_complete": 1,
"datetime_received": "2018-12-24 13:46:27",
"delivery_date": "2018-12-30",
"delivery_from": "10:30",
"delivery_to": "12:00",
"completed": "1",
"shopper": "joel"
}, {
"order_id": "336531",
"customer_name": "Tay",
"customer_surname": "Z",
"total_items": "0",
"completed_items": "0",
"percent_complete": 1,
"datetime_received": "2019-01-11 08:42:27",
"delivery_date": "2019-01-17",
"delivery_from": "10:30",
"delivery_to": "12:00",
"completed": "1",
"shopper": "joel"
}, {
"order_id": "336545",
"customer_name": "joel",
"customer_surname": "kabeya",
"total_items": "0",
"completed_items": "0",
"percent_complete": 1,
"datetime_received": "2019-01-17 19:00:27",
"delivery_date": "2019-01-18",
"delivery_from": "11:00",
"delivery_to": "12:00",
"completed": "0",
"shopper": "joel"
}, {
"order_id": "241918",
"customer_name": "Marietjie",
"customer_surname": "Short",
"total_items": "44",
"completed_items": "7",
"percent_complete": 0.15909090909091,
"datetime_received": "2018-07-25 15:18:25",
"delivery_date": "2018-10-29",
"delivery_from": "12:00",
"delivery_to": "13:00",
"completed": "0",
"shopper": "Tay"
}, {
"order_id": "281774",
"customer_name": "Ashleigh",
"customer_surname": "Hodge",
"total_items": "16",
"completed_items": "0",
"percent_complete": 0,
"datetime_received": "2018-10-04 15:59:19",
"delivery_date": "2018-10-29",
"delivery_from": "12:00",
"delivery_to": "13:00",
"completed": "0",
"shopper": null
}, {
"order_id": "336544",
"customer_name": "joel",
"customer_surname": "kabeya",
"total_items": "0",
"completed_items": "0",
"percent_complete": 1,
"datetime_received": "2019-01-17 08:00:27",
"delivery_date": "2019-01-19",
"delivery_from": "13:00",
"delivery_to": "14:00",
"completed": "0",
"shopper": "joel"
}]
}
所以我也想获取数组的长度,以访问诸如customer_name等的子属性
答案 0 :(得分:0)
您需要使用属性accessors。您可能还需要阅读Object Basics。
try
{
connectionn.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connectionn;
string query = "update LOGIN set pass='" + Npassword.Text + "' WHERE pass=@Npassword.Text";
command.CommandText = query;
command.ExecuteNonQuery();
MessageBox.Show("Password Changed");
connectionn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error, fill the fields required" + ex);
connectionn.Close();
}
答案 1 :(得分:0)
data
键的对象值内部是一个对象数组。因此,为了访问诸如customer_name
之类的任何键,可以使用dot (.)
或square[]
表示法。您可以按照以下link来找出两者之间的区别
let myData = {
"data": [{
"order_id": "336566",
"customer_name": "joel",
"customer_surname": "kabeya",
"total_items": "0",
"completed_items": "0",
"percent_complete": 1,
"datetime_received": "2019-01-21 15:00:27",
"delivery_date": "2019-01-24",
"delivery_from": "10:30",
"delivery_to": "12:00",
"completed": "1",
"shopper": "joel"
}, {
"order_id": "335719",
"customer_name": "joel",
"customer_surname": "kabeya",
"total_items": "0",
"completed_items": "0",
"percent_complete": 1,
"datetime_received": "2018-12-24 13:46:27",
"delivery_date": "2018-12-30",
"delivery_from": "10:30",
"delivery_to": "12:00",
"completed": "1",
"shopper": "joel"
}, {
"order_id": "336531",
"customer_name": "Tay",
"customer_surname": "Z",
"total_items": "0",
"completed_items": "0",
"percent_complete": 1,
"datetime_received": "2019-01-11 08:42:27",
"delivery_date": "2019-01-17",
"delivery_from": "10:30",
"delivery_to": "12:00",
"completed": "1",
"shopper": "joel"
}, {
"order_id": "336545",
"customer_name": "joel",
"customer_surname": "kabeya",
"total_items": "0",
"completed_items": "0",
"percent_complete": 1,
"datetime_received": "2019-01-17 19:00:27",
"delivery_date": "2019-01-18",
"delivery_from": "11:00",
"delivery_to": "12:00",
"completed": "0",
"shopper": "joel"
}, {
"order_id": "241918",
"customer_name": "Marietjie",
"customer_surname": "Short",
"total_items": "44",
"completed_items": "7",
"percent_complete": 0.15909090909091,
"datetime_received": "2018-07-25 15:18:25",
"delivery_date": "2018-10-29",
"delivery_from": "12:00",
"delivery_to": "13:00",
"completed": "0",
"shopper": "Tay"
}, {
"order_id": "281774",
"customer_name": "Ashleigh",
"customer_surname": "Hodge",
"total_items": "16",
"completed_items": "0",
"percent_complete": 0,
"datetime_received": "2018-10-04 15:59:19",
"delivery_date": "2018-10-29",
"delivery_from": "12:00",
"delivery_to": "13:00",
"completed": "0",
"shopper": null
}, {
"order_id": "336544",
"customer_name": "joel",
"customer_surname": "kabeya",
"total_items": "0",
"completed_items": "0",
"percent_complete": 1,
"datetime_received": "2019-01-17 08:00:27",
"delivery_date": "2019-01-19",
"delivery_from": "13:00",
"delivery_to": "14:00",
"completed": "0",
"shopper": "joel"
}]
}
console.log('Length of data array ', myData.data.length)
// this will get customer_name
myData.data.forEach(function(item) {
console.log(item.customer_name)
})
答案 2 :(得分:0)
由于最近我做了类似的事情,因此我可以帮助您开始一些工作。我不是专家之一,我也在学习。因此,我将您的阵列压缩为以下内容,仅用于测试。
然后,我提供了一个用于数组的基本for循环以及Object.values,它们仅显示数组中对象的属性。有许多种访问此数据的方法,但是经验丰富的人将能够提供更多帮助。
let data = [
{
order_id: "336566",
customer_name: "joel",
customer_surname: "kabeya",
total_items: "0"
},
{
order_id: "335719",
customer_name: "joel",
customer_surname: "kabeya",
total_items: "0",
completed_items: "0",
percent_complete: 1,
datetime_received: "2018-12-24 13:46:27"
},
{
order_id: "336531",
customer_name: "Tay",
customer_surname: "Z",
total_items: "0",
completed_items: "0",
percent_complete: 1
}
];
for(var i = 0; i < data.length; i++){
console.log(Object.values(data[i])); //or however you choose to display it
}
此外... data.length将为您提供对象数。我不确定如何获取键或属性的数量。
答案 3 :(得分:0)
嵌套的for in循环将为您解决问题。 希望这会有所帮助
var res = {
"data": [{
"order_id": "241918",
"customer_name": "Marietjie",
"customer_surname": "Short",
"total_items": "44",
"completed_items": "7",
"percent_complete": 0.15909090909091,
"datetime_received": "2018-07-25 15:18:25",
"delivery_date": "2018-10-29",
"delivery_from": "12:00",
"delivery_to": "13:00",
"completed": "0",
"shopper": "Tay"
},{
"order_id": "336544",
"customer_name": "joel",
"customer_surname": "kabeya",
"total_items": "0",
"completed_items": "0",
"percent_complete": 1,
"datetime_received": "2019-01-17 08:00:27",
"delivery_date": "2019-01-19",
"delivery_from": "13:00",
"delivery_to": "14:00",
"completed": "0",
"shopper": "joel"
}]
}
for(var char in res){
for(var obj in res[char]){
// Log all the customer names for example.
console.log(JSON.stringify(res[char][obj].customer_name));
}
}
对于in循环来说,这是一种循环对象的好方法。