admin管理员组

文章数量:1291517

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

My jQuery AJAX request looks like this:

 $("#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 protected]",
    "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.

Suggestions would be most wele.

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

My jQuery AJAX request looks like this:

 $("#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 protected]",
    "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.

Suggestions would be most wele.

Share edited Aug 4, 2018 at 11:07 Salman Arshad 272k84 gold badges443 silver badges534 bronze badges asked Apr 11, 2011 at 6:22 dangermarkdangermark 5442 gold badges5 silver badges17 bronze badges 1
  • if u r using php then json_encode is best solution for your data – xkeshav Commented Apr 11, 2011 at 7:01
Add a ment  | 

4 Answers 4

Reset to default 3

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:

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.

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.

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

{"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"]);

try this

<script type="text/javascript">
var json_string={
    "logged_in":true,
    "firstname":"Joe",
    "surname":"Bloggs",
    "Full_name":"Joe Bloggs",
    "email":"[email protected]",
    "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>

本文标签: javascriptAccessing data in a multidimensional JSON array with jQueryStack Overflow