Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 访问JSON响应中的字段_Javascript_Asp.net Mvc_Json_Asp.net Ajax - Fatal编程技术网

Javascript 访问JSON响应中的字段

Javascript 访问JSON响应中的字段,javascript,asp.net-mvc,json,asp.net-ajax,Javascript,Asp.net Mvc,Json,Asp.net Ajax,我有一个这样定义的类: // the Widget class has lots of properties I don't want to display // so I use the displayWidget class to carry just what I want to display public class displayWidget { public string WidgetId {get; set;} public string Description

我有一个这样定义的类:

// the Widget class has lots of properties I don't want to display
// so I use the displayWidget class to carry just what I want to display
public class displayWidget
{
    public string WidgetId {get; set;}
    public string Description {get; set;}

    public displayWidget(Widget widget)  
    {
        WidgetId = widget.WidgetId;
        Description = widget.Description;
    }
}
我有一个ActionResult方法,以::

var widgets = new List<displayWidget>();
foreach (var widget in matchingWidgets)
{
    widgets.Add(new displayWidget(widget));
}
return Json(widgets);
var widgets=newlist();
foreach(matchingWidgets中的var小部件)
{
添加(新的displayWidget(widget));
}
返回Json(小部件);
我的问题是,我不知道如何访问ajax中的WidgetId和Description属性。完成处理程序:

.done(
    function(response) {
        $('#widgetSection').html('');
        var html = '';
        var jsonData = JSON.parse(response);
        $.each(jsonData, 
            function (index, element) 
            { 
                $('body').append($('<div>', { text: element.WidgetId })); 
                $('body').append($('<div>', { text: element.Description }));
            });
    }
)
。完成(
功能(响应){
$('#widgetSection').html('');
var html='';
var jsonData=JSON.parse(响应);
各美元(jsonData,
函数(索引、元素)
{ 
$('body').append($('',{text:element.WidgetId}));
$('body').append($('',{text:element.Description}));
});
}
)

每个要输出WidgetId和描述的函数的内部应该是什么?

如果ActionResult返回一个数组,请尝试:

element[0].WidgetId
这将返回第一个结果,如果需要,您可以轻松地在列表中循环

编辑

正如@StephenMuecke所提到的,您不需要在这里使用
JSON.parse
,因为您已经返回了JSON数据,所以类似这样的内容足以循环遍历结果:

.done(
    function(response) {
        $('#widgetSection').html('');
        $.each(response, function (index, element) { 
            $('body').append(
                $('<div></div>').text(element.WidgetId )
            )
        });
    }
)
。完成(
功能(响应){
$('#widgetSection').html('');
$.each(响应、函数(索引、元素){
$('body')。追加(
$('').text(element.WidgetId)
)
});
}
)

What element.WidgetId/element.Description返回?我不知道。。。我知道响应包含我期望的内容,但现在JSON.parse(response)返回错误0x800a03f6-JavaScript运行时错误:无效字符dont'user JSON.parse。Try$.each(响应,函数(){});这其实是没有必要的。所需要的只是
$响应值已经是JSON,所以不应该对其进行解析,但他的问题是如何访问数据,这就是我所解释的。我假设如果他知道这是一个数组(我已经解释过了),他就可以从那里开始使用它。我会用这些信息更新答案,不过为了让其他用户更清楚。