如何通过Json获取对象内部的值

时间:2014-06-13 10:52:41

标签: c# javascript jquery .net json

我正在使用.NET MVC4

我使用了以下javascript函数:

function ShowDomainComponentDetail(compCode) {
        alert(compCode);
        $.ajax({
            url: "/PP/getDomainComponentDetailWithDomain",
            data: {
                'ComponentCode': compCode
            },
            dataType: "json",
            type: 'POST',
            cache: false,
            success: function (_responseData) {
                $('#divShowDomainCompDetail').show();
                alert(_responseData.Data)

            },
            error: function () {
                //
            }
        });
    }

成功后,我在.net中获得列表:

IdObservation=1, ObservationName="Started" , ObsType="Announced";
IdObservation=2, ObservationName="Not Started" , ObsType="Un Announced";
IdObservation=3, ObservationName="Declared" , ObsType="Announced";

我的问题是我不是abl; e访问Ajax sucess块中的这个列表。

我如何以下列方式访问此列表:

alert(_responseData.IdObservation);
alert(_responseData.ObservationName);

(此外我将把它分配给标签)。

请帮帮我。

编辑1:

我的Serverside函数返回列表:

public JsonResult getDomainComponentDetailWithDomain(string ComponentCode)
        {
            try
            {
                List<TEAMS_PP.Entity.correlations> compDetail_list = new correlation().getDomainComponentDetailswithDomain(ComponentCode);
                return Json(compDetail_list);
            }
            catch (Exception)
            {
                List<TEAMS_PP.Entity.correlations> BlankList = new List<TEAMS_PP.Entity.correlations>();
                return Json(BlankList);
            }
        }

2 个答案:

答案 0 :(得分:3)

将index用于数据对象,如下所示:

alert(_responseData[0].IdObservation);

循环遍历对象并获取每个对象的值。

答案 1 :(得分:1)

您可以使用$ each来迭代它

$.each(_responseData, function (key, value) {
                        var arr = value.IdObservation;
                    });