0

i have a json object and want to enumerate one object in this json object.

$.ajax({
    url: requrl,
    context: document.body,
    success: function(data){
        var obj = data['UserPenaltys'];
        for (var element in obj) {
            alert(element + ": " + obj[element]);
        }
    }
});

But it always returns only the first object data. What is wrong with my loop?

JSON Object:

data Object { Firstname="Max",Lastname="Mustermann",PenaltyCounter=16, 
        UserPenaltys Object { Penalty1="1.1", Penalty10="1.1", Penalty11="1.1"}
    }

UserPenaltys is what i want ...

Help this???

bsand
  • 135
  • 1
  • 2
  • 8
  • 2
    there's not much wrong with the loop - there must be something wrong with the object - please show it. – Alnitak Nov 01 '11 at 12:27
  • Is `obj` an array or you want to enumerate properties? – Jakub Konecki Nov 01 '11 at 12:29
  • possible duplicate of http://stackoverflow.com/questions/85992/how-do-i-enumerate-the-properties-of-a-javascript-object – Iulian Margarintescu Nov 01 '11 at 12:31
  • @JakubKonecki that code _should_ work for either, although it's not the recommended method for arrays. – Alnitak Nov 01 '11 at 12:32
  • 1
    @IulianMargarintescu not a duplicate IMHO - the OP does appear to know how to enumerate - the problem is his data, not the code. – Alnitak Nov 01 '11 at 12:35
  • @user: you haven't posted your JSON or JavaScript object. The data you have posted doesn't show us how it's defined in your code, so it doesn't help in the slightest. Please show the actual code defining `obj` and, if it comes from a source as JSON, the JSON data too. – Andy E Nov 01 '11 at 12:40
  • Could you please post the data in a format understandable to everyone? Where does this come from? It is not JavaScript or JSON. – Felix Kling Nov 01 '11 at 12:53
  • Do you need more informations? ... this is how i get the data in firebug – bsand Nov 01 '11 at 13:04
  • maybe your browser is blocking further alerts? have you tried writing object's data somewhere else? – maialithar Nov 01 '11 at 13:04
  • If i access data with " alert(obj.Penalty12); " it alerts the right value. – bsand Nov 01 '11 at 13:06
  • Data and loop was correct. it was a variable which was not defined. omg. thx for all the help – bsand Nov 01 '11 at 13:11
  • @user see this http://jsfiddle.net/aMnwt/1/ – Iulian Margarintescu Nov 01 '11 at 13:33

0 Answers0