1

我构建了一个 ASP.NET Web Api。现在,我已将项目更新为 MVC 4 RC,但动态对象不再按预期工作。

我有这个:

var Arr = new JsonArray();

foreach (var Post in Coll)
{
    dynamic Item = new JsonObject();
    Item.Header = Post.Header;
    Item.Body = Post.Body;
    Item.Language = Post.Language;
    Arr.Add(Item);
}

return Request.CreateResponse(HttpStatusCode.OK, Arr);

我从中得到的结果是(在浏览器中):

[{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]},{"Header":[],"Body":[],"Language":[]}]

在调试中我得到这个:(对象Arr)

{[{"Header":"All distances","Body":"All distances are ","Language":"EN"},{"Header":...

为什么不一样?

(Col 是一个对象列表)

4

3 回答 3

0

我猜你没有在客户端将 Arr 解析为 json 数据。在客户端ajax方法中试试这个:

 var obj = $.parseJSON(result);

result 是服务器返回的值。你可以得到数组的每个对象,如下所示:

        $(obj).each(function () {       
      // alert($(this).attr('Header'));  
 });
于 2013-09-27T06:33:15.520 回答
0

尝试使用它

返回 Request.CreateResponse(HttpStatusCode.OK, Arr.ToArray());

这只是一个猜测,如果您可以在调试中看到它,那么您可能会在添加到观察列表时强制计算对象并且观察者显示它。然后 .ToArray() 也应该对您有所帮助。

于 2013-09-26T12:39:38.030 回答
0

我有同样的问题,我已经修复了它以这种方式插入内容:

var response = req.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(JsonConvert.SerializeObject(MY_OBJECT, Formatting.Indented), Encoding.UTF8, "application/json");
return response;
于 2020-01-20T15:24:24.403 回答