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

本文关键字:例如 反序列化 数组 JSON | 更新日期: 2023-09-27 18:15:14

所以我在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"。模型。成就",因为该类型需要>JSON对象(例如{"name":"value"}(才能正确反序列化。

尝试反序列化到此对象时:

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";
}

很抱歉发了这么长又烦人的帖子,但我已经挣扎了一段时间,我真的希望有人能帮助我。

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

提供的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和Achiement之间进行分层。

{
    "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也不正确,我已经更正了