11

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.

3
  • Use console.log instead of alert (and check your browser console for the output). Result.customers is an array of objects, that's why alert shows what you're seeing. Commented May 3, 2013 at 18:05
  • 1
    From your sample data above, it looks like the customer name, for example, would be accessed using: Result.customers[i].customer.customerName. Your code only uses Result.customers[i].customerName. The name of the temp variable customer hides this subtlety. Commented May 3, 2013 at 18:08
  • @JimCote, your answer is not an "Answer" so I can't bump it up, but after three days of bashing my skull in, YOUR comment saved me! Commented Feb 13, 2017 at 8:39

2 Answers 2

10

Change

var customer = Result.customers[i];

to

var customer = Result.customers[i].customer;
0
1

You can handle the JSON object like:

for(var key in Result["customers"]) {
   // examples
   console.log( Result[key].customer );
   console.log( Result[key]["customer"] );
   console.log( Result[key]["customer"].customerID );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.