我要返回一个List <!> lt; <!> gt;从Web服务作为JSON对象列表。我试图使用for循环迭代列表并从属性中获取值。这是返回JSON的示例:

{"d":[{"__type":"FluentWeb.DTO.EmployeeOrder",
 "EmployeeName":"Janet Leverling",
 "EmployeeTitle":"Sales Representative",
 "RequiredDate":"\/Date(839224800000)\/",
 "OrderedProducts":null}]}

所以我试图用这样的东西提取内容:

function PrintResults(result) {

for (var i = 0; i < result.length; i++) { 
    alert(result.employeename);
}

应该怎么做?

有帮助吗?

解决方案

今天有同样的问题,你的主题帮了我,所以这里解决了;)

 alert(result.d[0].EmployeeTitle);

其他提示

小心,d是清单。

for (var i = 0; i < result.d.length; i++) { 
    alert(result.d[i].employeename);
}

很接近!试试这个:

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

更新

如果你的结果确实是一个对象的数组,那么你可能必须这样做:

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

或者,如果您想循环遍历数组中的每个结果,请尝试:

for (var i = 0; i < results.length; i++) {
    for (var prop in result[i]) {
        if (result[i].hasOwnProperty(prop)) {
            alert(result[i][prop]);
        }
    }
}

这是:

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

示例JSON文本:

{"title": "camp crowhouse", 
"key": "agtnZW90YWdkZXYyMXIKCxIEUG9zdBgUDA"}

由于你使用的是jQuery,你不妨使用每个方法...而且,似乎所有东西都是这个JS对象[Notation]中属性'd'的值。

$.each(result.d,function(i) {
    // In case there are several values in the array 'd'
    $.each(this,function(j) {
        // Apparently doesn't work...
        alert(this.EmployeeName);
        // What about this?
        alert(result.d[i][j]['EmployeeName']);
        // Or this?
        alert(result.d[i][j].EmployeeName);
    });
});

那应该有用。如果没有,那么也许你可以给我们一个更长的JSON例子。

编辑:如果这些内容都不起作用,那么我开始认为JSON的语法可能有问题。

var d = $.parseJSON(result.d);
for(var i =0;i<d.length;i++){
    alert(d[i].EmployeeName);
}

这样可行!

$(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" }]
                */
            }
        });
    });

主要的是使用与JSON键值对的属性完全相同的属性。

我有以下电话:

$('#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');
    });

从服务器返回的结构如下:

{"result":{"1":"waterskiing","2":"canoeing","18":"windsurfing"}}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top