jsonserializationException:无法对当前的JSON对象进行估算



我是C#和VisualStudio2017的新手,并且实际上已经陷入了数周之久。搜索网络,但找不到与我正确理解的结果。我要做的是从中获取JSON数据https://zkillboard.com/api/stats/characterid/224802743/然后将其转换为可用的数据,我可以在某些文本框中获取并显示某些数据。我使用https://quicktype.io/将json转换为c#array。

mainform.cs

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Net;
using ZKILLBOARDDATA;

namespace MainProgram
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            this.DoubleBuffered = true;
            this.SetStyle(ControlStyles.ResizeRedraw, true);
        }

        public async void Btn_submit_ClickAsync(object sender, EventArgs e)
        {
             var remoteUri = "https://zkillboard.com/api/stats/characterID/224802743/";
             var myWebClient = new WebClient();
             myWebClient.Headers.Add("user-agent", "C# App testing");
             var jsonString = await myWebClient.DownloadStringTaskAsync(remoteUri);
             var data = GettingStarted.FromJson(jsonString);
        }   
    }
}

Zkill.cs

using Newtonsoft.Json;
// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
//
//    using ZKILLBOARDDATA;
//
//    var data = GettingStarted.FromJson(jsonString);
//
namespace ZKILLBOARDDATA
{
    public partial class GettingStarted
    {
        [JsonProperty("attackers")]
        public Attacker[] Attackers { get; set; }
        [JsonProperty("killmail_id")]
        public long KillmailId { get; set; }
        [JsonProperty("killmail_time")]
        public string KillmailTime { get; set; }
        [JsonProperty("moon_id")]
        public long? MoonId { get; set; }
        [JsonProperty("solar_system_id")]
        public long SolarSystemId { get; set; }
        [JsonProperty("victim")]
        public Victim Victim { get; set; }
        [JsonProperty("war_id")]
        public long? WarId { get; set; }
        [JsonProperty("zkb")]
        public Zkb Zkb { get; set; }
    }
    public partial class Zkb
    {
        [JsonProperty("awox")]
        public bool Awox { get; set; }
        [JsonProperty("fittedValue")]
        public double FittedValue { get; set; }
        [JsonProperty("hash")]
        public string Hash { get; set; }
        [JsonProperty("locationID")]
        public long LocationID { get; set; }
        [JsonProperty("npc")]
        public bool Npc { get; set; }
        [JsonProperty("points")]
        public long Points { get; set; }
        [JsonProperty("solo")]
        public bool Solo { get; set; }
        [JsonProperty("totalValue")]
        public double TotalValue { get; set; }
    }
    public partial class Victim
    {
        [JsonProperty("alliance_id")]
        public long? AllianceId { get; set; }
        [JsonProperty("character_id")]
        public long? CharacterId { get; set; }
        [JsonProperty("corporation_id")]
        public long CorporationId { get; set; }
        [JsonProperty("damage_taken")]
        public long DamageTaken { get; set; }
        [JsonProperty("items")]
        public Item[] Items { get; set; }
        [JsonProperty("position")]
        public Position Position { get; set; }
        [JsonProperty("ship_type_id")]
        public long ShipTypeId { get; set; }
    }
    public partial class Position
    {
        [JsonProperty("x")]
        public double X { get; set; }
        [JsonProperty("y")]
        public double Y { get; set; }
        [JsonProperty("z")]
        public double Z { get; set; }
    }
    public partial class Item
    {
        [JsonProperty("flag")]
        public long Flag { get; set; }
        [JsonProperty("item_type_id")]
        public long ItemTypeId { get; set; }
        [JsonProperty("items")]
        public Item[] Items { get; set; }
        [JsonProperty("quantity_destroyed")]
        public long? QuantityDestroyed { get; set; }
        [JsonProperty("quantity_dropped")]
        public long? QuantityDropped { get; set; }
        [JsonProperty("singleton")]
        public long Singleton { get; set; }
    }
    public partial class Attacker
    {
        [JsonProperty("alliance_id")]
        public long? AllianceId { get; set; }
        [JsonProperty("character_id")]
        public long? CharacterId { get; set; }
        [JsonProperty("corporation_id")]
        public long? CorporationId { get; set; }
        [JsonProperty("damage_done")]
        public long DamageDone { get; set; }
        [JsonProperty("faction_id")]
        public long? FactionId { get; set; }
        [JsonProperty("final_blow")]
        public bool FinalBlow { get; set; }
        [JsonProperty("security_status")]
        public double SecurityStatus { get; set; }
        [JsonProperty("ship_type_id")]
        public long? ShipTypeId { get; set; }
        [JsonProperty("weapon_type_id")]
        public long? WeaponTypeId { get; set; }
    }
    public partial class GettingStarted
    {
        public static GettingStarted[] FromJson(string json) => JsonConvert.DeserializeObject<GettingStarted[]>(json, Converter.Settings);
    }
    public static class Serialize
    {
        public static string ToJson(this GettingStarted[] self) => JsonConvert.SerializeObject(self, Converter.Settings);
    }
    public class Converter
    {
        public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
        {
            MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
            DateParseHandling = DateParseHandling.None,
        };
    }
}

这是我从VS2017收到的错误消息。

jsonserializationException:无法对当前的JSON对象(例如{" name":" value"})键入类型'zkillboarddata.gettingstarted []',因为类型需要JSON阵列(例如[1,2,3])正确序列化。

要解决此错误要么将JSON更改为JSON数组(例如[1,2,3])或更改供应类型,因此它是正常的.NET类型(例如,不是像Integer这样的原始类型,而不是一个原始类型收集类型像数组或列表一样)可以从JSON对象中进行应对。也可以将jsonobjectAttribute添加到该类型中,以强迫其从JSON对象进行验证。

路径'alltimesum',第1行,位置14。

尝试此

     var remoteUri = "https://zkillboard.com/api/stats/characterID/224802743/";
     var myWebClient = new WebClient();
     myWebClient.Headers.Add("user-agent", "C# App testing");
     myWebClient.Headers.Add("content-type", "application/json");
     var jsonString = myWebClient.DownloadStringTaskAsync(remoteUri).Result;
     var obj = JsonConvert.DeserializeObject(jsonString);
     //OR
     var obj = JsonConvert.DeserializeObject<YourModel>(jsonString);

我认为您在模型中不需要这个

public partial class GettingStarted
    {
        public static GettingStarted[] FromJson(string json) => JsonConvert.DeserializeObject<GettingStarted[]>(json, Converter.Settings);
    }
    public static class Serialize
    {
        public static string ToJson(this GettingStarted[] self) => JsonConvert.SerializeObject(self, Converter.Settings);
    }
    public class Converter
    {
        public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
        {
            MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
            DateParseHandling = DateParseHandling.None,
        };
    }

我也有相同的问题;我通过使用新的默认System.Text.Json.JsonSerializer.Deserialize而不是Newtonsoft'S

解决了它
using System.Text.Json;    
var obj = JsonSerializer.Deserialize<YourMODEL>(jsonStr);

P.S。https://quicktype.io很棒(尽管对于C#似乎有些过时);我也尝试了http://json2csharp.com,但我的json文件很大,它冻结了。

有时(特别是对于深嵌套的复杂JSON)QuickType不会添加列表/数组类型;例如MyObj代替List<MyObj>。有时可以"解决"它首先选择t [],然后从网站的"生成菜单"选项中列出 Use T[] or List<T>

相关内容

  • 没有找到相关文章

最新更新