문제

Webservice에서 JSON 개체 목록으로 목록 <>을 반환하고 있습니다. 나는 루프를 사용하여 목록을 반복하고 속성에서 값을 가져 오려고 노력하고 있습니다. 이것은 돌아 오는 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 객체 [표기법]에서 모든 것이 속성 '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