Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Objects inside Array inside JSON object using Javascript

I have an encoded JSON object that stores an array of objects that I wish to iterate through in order to input into a database. The Result object is similar to as follows:

{
    "customers": [
        {
            "customer": {
                "id":"1",
                "customerName":"Customer Alpha",
                "customerID":" custA",
                "customerAddress":" Alpha Way",
                "customerCity":" Alpha",
                "customerState":" AL",
                "customerZip":"91605"
            }
        },
        {
            "customer": {
                "id":"2",
                "customerName":"Customer Beta",
                "customerID":" CustB",
                "customerAddress":" Beta Street",
                "customerCity":" Beta",
                "customerState":" BE",
                "customerZip":"91605"
            }
        }
    ]
}

I'd like to be able to input each field into the database, but the code I have inputs undefined into the database for everything. What is the proper way to access the variables stored in each field inside the array?

Here's what I'm using so far which doesn't work:

function insertCustomer(customerName, customerID, customerAddress, customerCity, customerState, customerZip) {
db.transaction(function (tx) {
    tx.executeSql('INSERT INTO Customers (customerName, customerID, customerAddress, customerCity, customerState, customerZip) VALUES (?, ?, ?, ?, ?, ?)', [customerName, customerID, customerAddress, customerCity, customerState, customerZip], CountReturns);
    });
};

        $.ajax({
      url      : 'http://webserver/retrieveDatabase.php',
      dataType : 'json',
      type     : 'get',
      success  : function(Result){
        alert(Result.customers);
        for (var i = 0, len = Result.customers.length; i < len; ++i) {
          var customer = Result.customers[i];
          insertCustomer(customer.customerName, customer.customerID, customer.customerAddress, customer.customerCity, customer.customerState, customer.customerZip);
        }
      }
    });

The alert responds with a series of [object Object]s.

like image 484
EzAnalyst Avatar asked May 03 '13 18:05

EzAnalyst


People also ask

How do you access an array inside a JSON object?

2.1. You can access the array values by using the index number: x = myObj. rights[0]; Program output.

How can I access and process nested objects arrays or JSON?

Use console. log or console. dir and inspect the structure of object / array. The property you are trying to access might be actually defined on a nested object / array.

Can a JSON array contain objects?

Yes, json arrays can contain any valid json string: objects with different key/value pairs, other arrays, numbers, strings, booleans all in the same array.

How do you access the elements of a JSON object?

To access the JSON object in JavaScript, parse it with JSON. parse() , and access it via “.” or “[]”.


1 Answers

Change

var customer = Result.customers[i];

to

var customer = Result.customers[i].customer;
like image 97
staramir Avatar answered Sep 28 '22 19:09

staramir