2013-05-03 11 views
8

Mam zakodowany obiekt JSON, który przechowuje tablicę obiektów, które chcę iterować, aby wprowadzić dane do bazy danych. Przedmiotem Rezultat jest podobny do następująco:Uzyskiwanie dostępu do obiektów wewnątrz Array wewnątrz obiektu JSON przy użyciu Javascript

{ 
    "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" 
      } 
     } 
    ] 
} 

Chciałbym, aby móc wprowadzić każde pole w bazie danych, ale kod mam wejść niezdefiniowane do bazy danych za wszystko. Jaki jest właściwy sposób dostępu do zmiennych przechowywanych w każdym polu wewnątrz tablicy?

Oto co używam do tej pory, która nie działa:

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); 
     } 
     } 
    }); 

Alarm reaguje z serii [object Object] s.

+0

użyć 'console.log' zamiast alarmu (i sprawdzić konsolę do przeglądarki wyjście). 'Result.customers' to tablica obiektów, dlatego alert pokazuje, co widzisz. – bfavaretto

+1

Z powyższych przykładowych próbek wygląda to tak, jak nazwa klienta, na przykład byłaby dostępna za pomocą: 'Result.customers [i] .customer.customerName'. Twój kod używa tylko 'Result.customers [i] .customerName'. Nazwa zmiennej tymczasowej 'customer' ukrywa tę subtelność. –

+0

@JimCote, twoja odpowiedź nie jest "odpowiedzią", więc nie mogę jej podważyć, ale po trzech dniach walenia w czaszkę Twój komentarz uratował mnie! – BillyNair

Odpowiedz

7

Zmień

var customer = Result.customers[i]; 

do

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

Yay dziękuję. To działa! =) – EzAnalyst

1

można obsługiwać obiekt JSON jak:

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