在MVC c#中序列化时无效的JSON

本文关键字:无效 JSON 序列化 MVC | 更新日期: 2023-09-27 18:17:10

我使用Jquery UI在我的网站上的文本框上有一个自动完成功能,JSON在MVC ViewModel中序列化,然后在视图中使用。JSON在运行时没有正确生成。浏览器内控制台返回以下

Uncaught SyntaxError: Unexpected token &

我还没有让它与我自己的数据一起工作,所以项目名称仍然与JqueryUI示例相关,与我自己的JSON数据。

The View (Razor)

<script>
  $(function () {
      var projects = @Model.TeamsAsJson

      $("#project").autocomplete({
          minLength: 0,
          source: projects,
          focus: function (event, ui) {
              $("#project").val(ui.item.TeamName);
              return false;
          },
          select: function (event, ui) {
              $("#project").val(ui.item.TeamName);
              $("#project-id").val(ui.item.TeamName);
              $("#project-description").html(ui.item.AreaName);
              $("#project-icon").attr("src", "images/" + ui.item.icon);
              return false;
          }
      })
      .autocomplete("instance")._renderItem = function (ul, item) {
          return $("<li>")
            .append("<a>" + item.TeamName + "<br>" + item.AreaName + "</a>")
            .appendTo(ul);
      };
  });
  </script>
<div id="project-label">Select a project (type "j" for a start):</div>
<img id="project-icon" src="images/transparent_1x1.png" class="ui-state-default"    alt="">
<input id="project">
<input type="hidden" id="project-id">
<p id="project-description"></p>

ViewModel

 public String TeamsAsJson{ get{
          JavaScriptSerializer Serializer = new JavaScriptSerializer();
          return Serializer.Serialize(TeamHelpers.GetAllTeams());
      }
          private set { TeamsAsJson = value; }
      }

序列化的JSON

[{&quot;TeamID&quot;:1,&quot;TeamName&quot;:&quot;Bilborough Broncos&quot;,&quot;ClubID&quot;:null,&quot;LeagueID&quot;:null,&quot;IsPremium&‌​quot;:false,&quot;PremiumLength&quot;:null,&quot;PremiumStart&quot;:null,&quot;Co‌​untryID&quot;:&quot;GB&quot;,&quot;AreaName&quot;:&quot;Nottingham&quot;,&quot;Cl‌​ub&quot;:null,&quot;League&quot;:null}]

[{"TeamID":1,"TeamName":"Bilborough Broncos","ClubID":null,"LeagueID":null,"IsPremium":false,"PremiumLength":null,"PremiumStart":null,"CountryID":"GB","AreaName":"Nottingham","Club":null,"League":null}]

在MVC c#中序列化时无效的JSON

c#变量正在被HTML编码。修复是:

var projects = @Html.Raw(Model.TeamsAsJson)

注意:这是从问题中提取出来的,并代表OP在这里发布。

答案1:

尝试使用JSON.parse作为:-

$("#project").autocomplete({
      minLength: 0,
      source: projects,
      focus: function (event, ui) {
          ui=JSON.parse(ui.replace(/&quot;/g,'"'));
          $("#project").val(ui.item.TeamName);
          return false;
      },
      select: function (event, ui) {
          ui=JSON.parse(ui.replace(/&quot;/g,'"'));
          $("#project").val(ui.item.TeamName);
          $("#project-id").val(ui.item.TeamName);
          $("#project-description").html(ui.item.AreaName);
          $("#project-icon").attr("src", "images/" + ui.item.icon);
          return false;
      }
  })

回答2:

ViewModel:

public String TeamsAsJson{ get{
          JavaScriptSerializer Serializer = new JavaScriptSerializer();
          return Serializer.Serialize(JsonConvert.SerializeObject(TeamHelpers.GetAllTeams(), Formatting.None));
      }
          private set { TeamsAsJson = value; }
      }