I think I am stupid ... but I cannot get my JSON output correctly

I am trying to create a shopping cart.

There's a very simple class that creates generic.dictionary(of string, generic.dictionary(of string, string)

contains my cart items.

The keys for the vocabulary are item IDs, so I can easily check if an item is in the cart using cartDictionary.ContainsKey(id)

and then increment or add a new one as needed.

BUY button trigger and AJAX-ified web method that returns data that looks like this:

{
    "d": {
        "7907": {
            "id": "7907",
            "qty": "4",
            "singlePrice": "1185"
        },
        "2698": {
            "id": "2698",
            "qty": "1",
            "singlePrice": "1322"
        }
    }
}

      

The initial element d

is created automatically by the AJAX message for reasons I don't understand, but it really doesn't matter, so my result data.d

in my AJAX success looks like this:

success: function (data) {
    result = [data.d];
}

      

Now, from this I should be able to get the internal data to submit to the cart

So I need to be able to loop through the items by ID and retrieve

id
qty
singlePrice

      

So that I can display it in the browser, but I'm going to cross myself trying to figure it out.

I tried adding [

around data.d like

var result = [data.d]

      

and tried to loop through 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
         });
     });

      

On the returns from the warnings, I'm clearing up getting some JSON objects, but I'm obviously confusing something!

I'm not a programmer, but I'm working on a project that is slowly driving me crazy!

+3


source to share


4 answers


You must do

var data = {
    "d": {
        "7907": {
            "id": "7907",
            "qty": "4",
            "singlePrice": "1185"
        },
        "2698": {
            "id": "2698",
            "qty": "1",
            "singlePrice": "1322"
        }
    }
}



var result = data.d;

$.each(result , function(ind, el) {
    //alert(thing); //<< returns object object
    alert(ind + " - " + el.id);
});

      



fiddle here http://jsfiddle.net/VHPQX/

+1


source


success: function (data) {
    var d = $.parseJSON(data.d);
    for(var i =0;i<d.length;i++)
    {     
        alert("Id is"+d[i].id);
    }
}

      



+1


source


You need to use the jQuery generic iterator$.each()

, not its jQuery object iterator $().each()

:

success: function (data) {
    $.each(data.d, function(k, val) {
       // "val" is the current item, so use its properties here, e.g.:
       var id = val.id,
           total = +val.qty * +val.singlePrice;
    });
}

      

(Simple demo: http://jsfiddle.net/2vvfZ/ )

Note that when iterating over the properties of an object, the order is not guaranteed (and when I checked this in Chrome it made item 2698 and then item 7907). If you need to be sure of ordering, use an array of objects.

0


source


You can just use a for..in loop:

for (key in data.d)
{
    document.write(key + '<br>')
    document.write('<blockquote>')
    document.write('id: ' + data.d[key].id + '<br>')            
    document.write('qty: ' + data.d[key].qty + '<br>')            
    document.write('singlePrice: ' + data.d[key].singlePrice + '<br>')            
    document.write('</blockquote>')
}

      

Result:

2698
    id: 2698
    qty: 1
    singlePrice: 1322
7907
    id: 7907
    qty: 4
    singlePrice: 1185

      

JSFiddle: http://jsfiddle.net/qnqWK/

0


source







All Articles