使用jQuery访问多维JSON数组中的数据

时间:2021-07-10 15:56:01

I am trying to work out how to access data in an essentially multidimensional JSON array.

我试图找出如何访问基本上多维JSON数组中的数据。

My jQuery AJAX request looks like this:

我的jQuery AJAX请求如下所示:

 $("#login-form").submit(function(e) {
 e.preventDefault();
 $.ajax({
   type: 'POST',
   url: '/ajax/login',
   data: 'email='+$("#email").val()+'&password='+$("#password").val(),
   success: function(data){

     // FIRE ALERT HERE        
     alert(data.firstname);

     },
     dataType: 'json'
  });
});

This is what i am getting back. User account details, plus a list of products they have against their account.

这就是我要回来的。用户帐户详细信息,以及他们针对其帐户的产品列表。

{
    "logged_in":true,
    "firstname":"Joe",
    "surname":"Bloggs",
    "Full_name":"Joe Bloggs",
    "email":"email@website.com",
    "phone":"+123456789",
    "website":"",
    "age":"26-35",
    "street":"1 Street Ave",
    "city":"Townland",
    "state":"NA",
    "postcode":"1234",
    "country":"Australia",
    "products":2,
    "0":{
        "product_no":"1087",
        "customer":"2",
        "bought_from":"1",
        "date_of_purchase":"2011-04-08",
        "method":"instore",
        "invoice":"0",
        "current":"1"
    },
    "1":{
        "product_no":"24",
        "customer":"2",
        "bought_from":"1",
        "date_of_purchase":"2011-04-08",
        "method":"instore",
        "invoice":"0",
        "current":"1"
    }
}

As you can see, i am alerting the first name, which is fine. I can access everything in the first dimension by using data.key but i'm not sure how then i need to index the next dimension. Obviously I would like to display each of the products somehow.

正如你所看到的,我正在警告第一个名字,这很好。我可以通过使用data.key访问第一维中的所有内容,但我不知道如何我需要索引下一个维度。显然我想以某种方式展示每个产品。

Suggestions would be most welcome.

建议是最受欢迎的。

4 个解决方案

#1


2  

Inside your success function you can treat the JSON data as a JavaScript object. You can access the product array and objects inside it like this:

在您的成功函数中,您可以将JSON数据视为JavaScript对象。您可以像这样访问其中的产品数组和对象:

console.log(data.products + " product(s) in data"); // data.products is 2 (integer)
for(var i = 0; i < data.products; i++) {            // 
    var product = data[i.toString()];               // 0.toString() is "0"
                                                    // data["0"] is what you want
                                                    // now product points to the property "0"
    console.log(product.product_no);                // so you can use product.xxx
                                                    // or product["xxx"]
}                                                   // likewise for "1", "2", "3" and so on

Replace console.log with alert if you do not know what console is.

如果您不知道控制台是什么,请将alert.log替换为alert。

#2


1  

Each of the product details can be accessed through data[iProductIndex.toString()] member. Data is stored inside data["0"] and data["1"], therefore to access them you need to convert integer value to string. Unfortunately you won't be able to use $.each loop because "0" and "1" are separate member objects. Use for loop with iProductIndex.

可以通过数据[iProductIndex.toString()]成员访问每个产品详细信息。数据存储在数据[“0”]和数据[“1”]中,因此要访问它们,您需要将整数值转换为字符串。不幸的是,您将无法使用$ .each循环,因为“0”和“1”是单独的成员对象。用于与iProductIndex循环。

#3


0  

Data supplied does not allow for your answer, Salman A. See JSON Arrays for array definition, to have it work your way it must've been defined as

提供的数据不允许您的答案,Salman A.请参阅JSON Arrays进行数组定义,让它按照您的方式工作,它必须被定义为

{"products" : [ {"product_no":"1087",
        "customer":"2",
        "bought_from":"1",
        "date_of_purchase":"2011-04-08",
        "method":"instore",
        "invoice":"0",
        "current":"1"} ] }

To OP: alert(data["0"].product_no); alert(data["1"]["date_of_purchase"]);

至OP:alert(data [“0”]。product_no);警报(数据[ “1”] [ “date_of_purchase”]);

#4


0  

try this

尝试这个

<script type="text/javascript">
var json_string={
    "logged_in":true,
    "firstname":"Joe",
    "surname":"Bloggs",
    "Full_name":"Joe Bloggs",
    "email":"email@website.com",
    "phone":"+123456789",
    "website":"",
    "age":"26-35",
    "street":"1 Street Ave",
    "city":"Townland",
    "state":"NA",
    "postcode":"1234",
    "country":"Australia",
    "products":2,
    "0":{
        "product_no":"1087",
        "customer":"2",
        "bought_from":"1",
        "date_of_purchase":"2011-04-08",
        "method":"instore",
        "invoice":"0",
        "current":"1"
    },
    "1":{
        "product_no":"24",
        "customer":"2",
        "bought_from":"1",
        "date_of_purchase":"2011-04-08",
        "method":"instore",
        "invoice":"0",
        "current":"1"
    }
};

for (key in json_string) {
// Most modern browsers should have hasOwnProperty by now.
// This keeps us from getting farther up the chain.
if (json_string.hasOwnProperty(key)) {
document.write(key + "->" + json_string[key]);
document.write("<br>");
}
}; 


var pro_1= json_string[0]; // here u change 0 with 1 and get the data of "1"

for (key in pro_1) {
if (pro_1.hasOwnProperty(key)) {
document.write(key + "->" + pro_1[key]);
document.write("<br>");
}
}; 

</script>

#1


2  

Inside your success function you can treat the JSON data as a JavaScript object. You can access the product array and objects inside it like this:

在您的成功函数中,您可以将JSON数据视为JavaScript对象。您可以像这样访问其中的产品数组和对象:

console.log(data.products + " product(s) in data"); // data.products is 2 (integer)
for(var i = 0; i < data.products; i++) {            // 
    var product = data[i.toString()];               // 0.toString() is "0"
                                                    // data["0"] is what you want
                                                    // now product points to the property "0"
    console.log(product.product_no);                // so you can use product.xxx
                                                    // or product["xxx"]
}                                                   // likewise for "1", "2", "3" and so on

Replace console.log with alert if you do not know what console is.

如果您不知道控制台是什么,请将alert.log替换为alert。

#2


1  

Each of the product details can be accessed through data[iProductIndex.toString()] member. Data is stored inside data["0"] and data["1"], therefore to access them you need to convert integer value to string. Unfortunately you won't be able to use $.each loop because "0" and "1" are separate member objects. Use for loop with iProductIndex.

可以通过数据[iProductIndex.toString()]成员访问每个产品详细信息。数据存储在数据[“0”]和数据[“1”]中,因此要访问它们,您需要将整数值转换为字符串。不幸的是,您将无法使用$ .each循环,因为“0”和“1”是单独的成员对象。用于与iProductIndex循环。

#3


0  

Data supplied does not allow for your answer, Salman A. See JSON Arrays for array definition, to have it work your way it must've been defined as

提供的数据不允许您的答案,Salman A.请参阅JSON Arrays进行数组定义,让它按照您的方式工作,它必须被定义为

{"products" : [ {"product_no":"1087",
        "customer":"2",
        "bought_from":"1",
        "date_of_purchase":"2011-04-08",
        "method":"instore",
        "invoice":"0",
        "current":"1"} ] }

To OP: alert(data["0"].product_no); alert(data["1"]["date_of_purchase"]);

至OP:alert(data [“0”]。product_no);警报(数据[ “1”] [ “date_of_purchase”]);

#4


0  

try this

尝试这个

<script type="text/javascript">
var json_string={
    "logged_in":true,
    "firstname":"Joe",
    "surname":"Bloggs",
    "Full_name":"Joe Bloggs",
    "email":"email@website.com",
    "phone":"+123456789",
    "website":"",
    "age":"26-35",
    "street":"1 Street Ave",
    "city":"Townland",
    "state":"NA",
    "postcode":"1234",
    "country":"Australia",
    "products":2,
    "0":{
        "product_no":"1087",
        "customer":"2",
        "bought_from":"1",
        "date_of_purchase":"2011-04-08",
        "method":"instore",
        "invoice":"0",
        "current":"1"
    },
    "1":{
        "product_no":"24",
        "customer":"2",
        "bought_from":"1",
        "date_of_purchase":"2011-04-08",
        "method":"instore",
        "invoice":"0",
        "current":"1"
    }
};

for (key in json_string) {
// Most modern browsers should have hasOwnProperty by now.
// This keeps us from getting farther up the chain.
if (json_string.hasOwnProperty(key)) {
document.write(key + "->" + json_string[key]);
document.write("<br>");
}
}; 


var pro_1= json_string[0]; // here u change 0 with 1 and get the data of "1"

for (key in pro_1) {
if (pro_1.hasOwnProperty(key)) {
document.write(key + "->" + pro_1[key]);
document.write("<br>");
}
}; 

</script>