无法反序列化当前的JSON数组(例如[1,2,3])

时间:2016-10-27 19:33:27

标签: c# json asp.net-mvc api razor

所以我正在研究Overwatch API,我一直在努力展示这个JSON的成就:

{
totalNumberOfAchievements: 67,
numberOfAchievementsCompleted: 57,
finishedAchievements: "57 / 67",
- achievements: [
{
name: "?",
finished: true,
image: "https://overwatch-a.akamaihd.net/img/icons/0x0E6000000000015D-0e20595aad6b8ccf2de427df797ea1a78ba7dbe412cc54a0e5add73aaaca80bc7d5322859afa7e4da33816fe8ed89ee2c2f0c97c0ae87e5e5f32f29df989e21f.png",
description: "...",
category: "General"
},
{
name: "Centenary",
finished: true,
image: "https://blzgdapipro-a.akamaihd.net/game/achievements/0x0E60000000000156.png",
description: "Win 100 games in Quick or Competitive Play.",
category: "General"
},

列表继续......

我收到此错误:

  

附加信息:无法将当前JSON数组(例如> [1,2,3])反序列化为“Overwatch.Models.Achievements”类型,因为该类型需要> JSON对象(例如{“name”:“值“})正确反序列化。

尝试反序列化此对象时:

public class OWAchievements
    {
        public int totalNumberOfAchievements { get; set; }
        public int numberOfAchievementsCompleted { get; set; }
        public string finishedAchievements { get; set; }
        public Achievements achievements { get; set; }
    }

public class Achievements
    {
        public IEnumerable<Achievements> achievements { get; set; }
        public string name { get; set; }
        public bool finished { get; set; }
        public string image { get; set; }
        public string description { get; set; }
        public string category { get; set; }
    }

通过我的控制器:

[HttpPost]
        public ActionResult Index(string profileId)
        {
            var json = new WebClient().DownloadString($"https://api.lootbox.eu/pc/eu/{profileId}/achievements");
            var data = JsonConvert.DeserializeObject<OWAchievements>(json);
            ViewBag.Tag = profileId;
            return View(data);
        }

最后在我看来显示它:

@model IEnumerable<Overwatch.Models.OWAchievements>

@{
    ViewBag.Title = "Index";
}

<h2>Enter battle-tag</h2>

<div class="row">
    <div class="col-md-12">
        @using (Html.BeginForm())
        {
            @Html.TextBox("profileId", null, new { @class = "form-control" }); <br />
            <button class="btn btn-primary" type="submit"> Find Player </button>
        }
    </div>
</div>
@if (Model != null)
{

    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.totalNumberOfAchievements)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.numberOfAchievementsCompleted)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.finishedAchievements)
            </th>
            <th></th>
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.totalNumberOfAchievements)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.numberOfAchievementsCompleted)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.finishedAchievements)
                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
                    @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
                    @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
                </td>
            </tr>
        }

    </table>
}
else
{
    ViewBag.Text = "Nothing";
}

对不起这篇漫长而烦人的帖子感到抱歉,但我已经挣扎了一段时间了,我真的希望有人可以帮助我。

2 个答案:

答案 0 :(得分:1)

如果JSON缺少关闭]和}。

我修好后,尝试了以下课程:

public class Rootobject
{
    public int totalNumberOfAchievements { get; set; }
    public int numberOfAchievementsCompleted { get; set; }
    public string finishedAchievements { get; set; }
    public Achievement[] achievements { get; set; }
}

public class Achievement
{
    public string name { get; set; }
    public bool finished { get; set; }
    public string image { get; set; }
    public string description { get; set; }
    public string category { get; set; }
}

请不要在RootObject和Achievment之间进行层次分析。

答案 1 :(得分:0)

{
    "totalNumberOfAchievements": 67,
    "numberOfAchievementsCompleted": 57,
    "finishedAchievements": "57 / 67",
    "achievements": [{
        "name": "?",
        "finished": true,
        "image": "https://overwatch-a.akamaihd.net/img/icons/0x0E6000000000015D-0e20595aad6b8ccf2de427df797ea1a78ba7dbe412cc54a0e5add73aaaca80bc7d5322859afa7e4da33816fe8ed89ee2c2f0c97c0ae87e5e5f32f29df989e21f.png",
        "description": "...",
        "category": "General"
    }, {
        "name": "Centenary",
        "finished": true,
        "image": "https://blzgdapipro-a.akamaihd.net/game/achievements/0x0E60000000000156.png",
        "description": "Win 100 games in Quick or Competitive Play.",
        "category": "General"
    }]
}

这将是你的班级结构:

public class Achievement
{
    public string name { get; set; }
    public bool finished { get; set; }
    public string image { get; set; }
    public string description { get; set; }
    public string category { get; set; }
}

public class Achievements
{
    public int totalNumberOfAchievements { get; set; }
    public int numberOfAchievementsCompleted { get; set; }
    public string finishedAchievements { get; set; }
     public List<Achievement> achievements { get; set; }
}

您的Json也不正确,我已经纠正了