【问题标题】:How to get values inside object got through Json如何通过Json获取对象内部的值
【发布时间】:2014-06-13 10:52:41
【问题描述】:

我正在使用 .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";

我的问题是我无法在 Ajax 成功块中访问此列表。

我如何访问此列表:

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

(接下来我将把它分配给标签)。

请帮帮我。

编辑 1:

我的服务器端函数返回列表:

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);
            }
        }

【问题讨论】:

  • 如何从服务器返回列表?
  • @James 请看 EDIT1
  • Cool 只是确保您正确地发回它,您的结果是一个简单的对象数组(如果您使用调试器,您可以看到)。请参阅下面@Bhushan 的回答。
  • @James 是的,答案有效!!!感谢您与我的问题分享时间:)

标签: c# javascript jquery .net json


【解决方案1】:

将索引与数据对象一起使用,如下所示:

alert(_responseData[0].IdObservation);

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

【讨论】:

  • tumacha swagat aahe :)
【解决方案2】:

你可以使用 $each 来迭代它

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    相关资源
    最近更新 更多