我是C#和Visualstudio2017的新手,并且已经坚持了几周。搜索网但没有找到与此相关的结果,我可以正确理解。我想要做的是从中获取json数据 https://zkillboard.com/api/stats/characterID/224802743/ 然后将其转换为可以获取的可用数据,并在某些文本框中显示某些数据。我使用https://quicktype.io/将json转换为C#数组。
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对象(例如{&#34; name&#34;:&#34; value&#34;})反序列化为类型&#39; ZKILLBOARDDATA.GettingStarted []&#39;因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化。
要修复此错误,请将JSON更改为JSON数组(例如[1,2,3])或更改反序列化类型,使其成为普通的.NET类型(例如,不是像整数这样的基本类型,而不是可以从JSON对象反序列化的集合类型,如数组或List)。 JsonObjectAttribute也可以添加到类型中以强制它从JSON对象反序列化。
路径&#39; allTimeSum&#39;,第1行,第14位。
答案 0 :(得分:0)
试试这个
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,
};
}
答案 1 :(得分:0)
我遇到了同样的问题;我通过使用新的默认 where (@departmentID is null) or
(@divisionID is null and DepartmentID = @departmentID) or
(DepartmentID = @departmentID and DivisionID = @divisionID)
而不是 System.Text.Json.JsonSerializer.Deserialize
's
Newtonsoft
附言https://quicktype.io 很棒(虽然对于 C# 来说似乎有些过时);我也试过 http://json2csharp.com 但我的 json 文件很大而且它冻结了。
有时(特别是对于深度嵌套的复杂 json)quicktype 不添加列表/数组类型;例如using System.Text.Json;
var obj = JsonSerializer.Deserialize<YourMODEL>(jsonStr);
而不是 MyObj
。
有时可以通过先选择 T[] 然后从网站的生成菜单选项 List<MyObj>
中列出来“修复”它。