Loop through JSON object List

后端 未结 8 1970
忘掉有多难
忘掉有多难 2020-11-28 20:51

I am returning a List<> from a webservice as a List of JSON objects. I am trying to use a for loop to iterate through the list and grab the values out of the properties.

相关标签:
8条回答
  • 2020-11-28 21:06

    Here it is:

    success: 
        function(data) {
            $.each(data, function(i, item){
                alert("Mine is " + i + "|" + item.title + "|" + item.key);
            });
        }
    

    Sample JSON text:

    {"title": "camp crowhouse", 
    "key": "agtnZW90YWdkZXYyMXIKCxIEUG9zdBgUDA"}
    
    0 讨论(0)
  • 2020-11-28 21:10
    var d = $.parseJSON(result.d);
    for(var i =0;i<d.length;i++){
        alert(d[i].EmployeeName);
    }
    
    0 讨论(0)
  • 2020-11-28 21:11

    I have the following call:

    $('#select_box_id').change(function() {
            var action = $('#my_form').attr('action');
        $.get(action,{},function(response){
            $.each(response.result,function(i) {
    
                alert("key is: " + i + ", val is: " + response.result[i]);
    
            });
        }, 'json');
        });
    

    The structure coming back from the server look like:

    {"result":{"1":"waterskiing","2":"canoeing","18":"windsurfing"}}
    
    0 讨论(0)
  • 2020-11-28 21:12

    Be careful, d is the list.

    for (var i = 0; i < result.d.length; i++) { 
        alert(result.d[i].employeename);
    }
    
    0 讨论(0)
  • 2020-11-28 21:25

    This will work!

    $(document).ready(function ()
        {
            $.ajax(
                {
                type: 'POST',
                url: "/Home/MethodName",
                success: function (data) {
                    //data is the string that the method returns in a json format, but in string
                    var jsonData = JSON.parse(data); //This converts the string to json
    
                    for (var i = 0; i < jsonData.length; i++) //The json object has lenght
                    {
                        var object = jsonData[i]; //You are in the current object
                        $('#olListId').append('<li class="someclass>' + object.Atributte  + '</li>'); //now you access the property.
    
                    }
    
                    /* JSON EXAMPLE
                    [{ "Atributte": "value" }, 
                    { "Atributte": "value" }, 
                    { "Atributte": "value" }]
                    */
                }
            });
        });
    

    The main thing about this is using the property exactly the same as the attribute of the JSON key-value pair.

    0 讨论(0)
  • 2020-11-28 21:26

    It's close! Try this:

    for (var prop in result) {
        if (result.hasOwnProperty(prop)) {
            alert(result[prop]);
        }
    }
    

    Update:

    If your result is truly is an array of one object, then you might have to do this:

    for (var prop in result[0]) {
        if (result[0].hasOwnProperty(prop)) {
            alert(result[0][prop]);
        }
    }
    

    Or if you want to loop through each result in the array if there are more, try:

    for (var i = 0; i < results.length; i++) {
        for (var prop in result[i]) {
            if (result[i].hasOwnProperty(prop)) {
                alert(result[i][prop]);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题