【问题标题】:Why is my JSON response being returned surrounded with square brackets[]?为什么我的 JSON 响应被方括号 [] 包围?
【发布时间】:2015-11-01 15:19:24
【问题描述】:

我试图弄清楚为什么我的 JSON 响应被方括号括起来。我正在使用 ASP.NET Web API 和 Angular。我在想这就是我的 Angular 代码没有打印到 HTML 的原因。

namespace GeekQuiz.Controllers
{
    public class TrialController : ApiController
    {
        private TriviaContext db = new TriviaContext();

        // GET api/trial
        public IQueryable<TriviaQuestion> GetTriviaQuestions()
        {
            return db.TriviaQuestions;
        }
    }
}

上面的代码来自我的TrialController,我正在从 Angular 打个电话。

var app = angular.module('myApp', []);
app.controller('questCtrl', function ($scope, $http) {
    $http.get("/api/trial").success(function (data, status, headers, config) {
        $scope.options = data.options;
    }).error(function (data, status, headers, config) {
        $scope.title = "Oops... something went wrong";
        $scope.working = false;
    });
});

这是我的查看页面:

@{
    ViewBag.Title = "Contact";
}
<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>

<div ng-app="myApp" ng-controller="questCtrl">

    <table>
        <tr ng-repeat="option in options">
            <td>{{ option.id }}</td>
            <td>{{ option.title }}</td>
            <td>{{ option.questionId }}</td>
        </tr>
    </table>

</div>

<address>
    One Microsoft Way<br />
    Redmond, WA 98052-6399<br />
    <abbr title="Phone">P:</abbr>
    425.555.0100
</address>

<address>
    <strong>Support:</strong>   <a href="mailto:Support@example.com">Support@example.com</a><br />
    <strong>Marketing:</strong> <a href="mailto:Marketing@example.com">Marketing@example.com</a>
</address>
@section scripts {
    @Scripts.Render("~/Scripts/angular.js")
    @Scripts.Render("~/Scripts/app/quest-controller.js")
}

我正在使用 ASP.NET 并尝试学习如何使用 Angular 和 .NET 构建一个安静的应用程序。我正在使用本教程angular with ASP.NET 和 ASP.NET 上的 Lynda.com 课程。

这是我的 JSON 响应。我如何获得方括号?我不知道他们为什么在那里。

[
  {
    "options":[
      {"id":1,"title":"2000","questionId":1},
      {"id":2,"title":"2001","questionId":1},
      {"id":3,"title":"2002","questionId":1},
      {"id":4,"title":"2003","questionId":1}],
    "id":1,
    "title":"When was .NET first released?"
  },
  {
    "options":[
      {"id":5,"title":"Contoso Ltd.","questionId":2},
      {"id":175,"title":"System.DateTime","questionId":44},
      {"id":176,"title":"System.Float","questionId":44}
    ],
    "id":44,
    "title":"Which of the following is NOT a value type in the .NET Framework Common Type System?"
  }
]

这是一个成功的 JSON 响应的样子:

{
  "options":[
    {"id":85,"title":"DOS","questionId":22},
    {"id":86,"title":"Altair Basic","questionId":22},
    {"id":87,"title":"PC Basic","questionId":22},
    {"id":88,"title":"Windows","questionId":22}
  ],
  "id":22,
  "title":"What was Microsoft's first product?"
}

也许这会有所帮助:

namespace GeekQuiz.Models
{
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

    public class TriviaQuestion
    {
        public int Id { get; set; }

        [Required]
        public string Title { get; set; }

        public virtual List<TriviaOption> Options { get; set; }
    }
}

这段代码是 TriviaQuestion 模型,也许我没有正确对待它。 如果有任何出色的 .NET 程序员正在寻找门徒,请告诉我。

【问题讨论】:

  • 您的代码中有错字,您定义了$scope.options,但使用option.,将其更改为options.,如options.id等...
  • Tushar,option.id 中的单个选项是每个循环的内置角度,因此它只是循环的局部变量,我声明为对象名称,然后返回属性那个变量。
  • 是的,错过了 ng-repeat 抱歉,你能告诉我控制台日志吗,AFAIK,响应返回 data ,对你来说它是 data.data.options .. 但首先要得出一些结论,我需要 c 你的控制台日志

标签: javascript c# asp.net json angularjs


【解决方案1】:

因为IEnumerable&lt;T&gt;被序列化为JSON数组。

关于 Angular 部分...

我猜$scope.options = data.options; 是错误的,因为您是从 WebAPI 返回数组本身,而我找不到理由认为您的 options 正在一个对象中被序列化其中包含一个属性 options但您的数据似乎以 JSON 数组的形式返回原样

如果您将所谓的赋值转换为$scope.options = data;,您的绑定可能会起作用。

【讨论】:

  • 感谢您的建议,但这似乎不起作用。我不知道是不是因为在 TriviaQuestions.cs 模型类中,来自 TriviaOptions 的列表正在通过。我真的只是想简化教程,以便我可以从头开始理解它并构建一个简单的 asp.net/angular/restful 应用程序。
  • @Dlaugh14 您想如何在不使用 JSON 数组的情况下发送多个琐事问题?
  • Fidemraizer 我想这是个好问题。我想我最初认为列表选项不存在。我现在正试图弄清楚如何通过数组正确发送问题,但我只是想举一个简单的例子,因为我没有过多地使用数组。如果 TriviaOption 不存在,我是否需要通过数组发送它?
  • @Dlaugh14 好吧,您可以发送一个空列表或使用ng-if 并检查问题是否具有选项属性
猜你喜欢
  • 2021-12-13
  • 2013-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-05
  • 1970-01-01
  • 2016-06-04
相关资源
最近更新 更多