I’m trying to build a shopping cart.
There’s very simple class which creates a generic.dictionary(of string, generic.dictionary(of string, string) this contains my cart items.
The keys for the intitial dictionary are the item Ids, so I can easily check whether the item is already in the cart using cartDictionary.ContainsKey(id) and then increment the quantity or add a new as required.
The BUY button triggers and AJAX-ified web method, which returns data looking like this:
{
"d": {
"7907": {
"id": "7907",
"qty": "4",
"singlePrice": "1185"
},
"2698": {
"id": "2698",
"qty": "1",
"singlePrice": "1322"
}
}
}
The initial item d is created automatically by the AJAX post for reasons which I dont understand, but it doesnt really matter, my output is therefore data.d in my AJAX success, as follows:
success: function (data) {
result = [data.d];
}
Now, from this I need to be able to get the inner data to present it into a shopping cart
So I need to be able to loop through the items by ID and extract
id
qty
singlePrice
So that I can display it in the browser, but I’m going cross eyed trying to work it out.
I’ve tried adding [ around data.d, like
var result = [data.d]
and tried looping within the result like
result = [data.d];
$(result).each(function (i, thing) {
var thisOne = (result[i]);
//alert(thing); //<< returns object object
$(thisOne).each(function (j, val) {
alert(thisOne + " - " + val.id); //<< both thisOne and val.id return object object
});
});
By the returns from the alerts I am clearing getting some kind of JSON objects, but I’ve obviously got something mixed up!
I’m no programmer, but working on a project which is slowly driving me crazy!!
You should do
fiddle here http://jsfiddle.net/VHPQX/