저는 C#과 Visualstudio2017에 매우 익숙하며 문자 그대로 몇 주 동안 붙어 있습니다. 그물을 검색하고 있지만 이것에 관한 결과를 찾지 못해서 제대로 이해할 수 있습니다. 내가 뭘 하려는지 https://zkillboard.com/api/stats/characterID/224802743/ 에서 json 데이터를 가져 와서 특정 텍스트 상자에 특정 데이터를 표시 할 수있는 사용 가능한 데이터로 변환하는 것입니다. 나는 json을 C# 배열로 변환하기 위해 https://quicktype.io/을 사용했다.JsonSerializationException : 현재 JSON 객체를 비 직렬화 할 수 없습니다.
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 : 예 [1,2,3 (타입으로 'ZKILLBOARDDATA.GettingStarted는 [] 유형이 JSON 배열을 필요로하기 때문에 (예 : { "값", "이름"}) 현재 JSON 객체를 직렬화 할 수 없다 ])를 사용하여 올바르게 deserialize하십시오.
JSON 배열을 JSON 배열 (예 : [1,2,3])로 변경하거나 비 직렬화 된 형식을 일반 .NET 유형 (예 : 정수와 같은 기본 유형이 아니라 변경할 수 있습니다. JSON 객체에서 직렬화 될 수있는 배열 또는 List와 같은 콜렉션 유형). JsonObjectAttribute를 유형에 추가하여 강제로 JSON 객체에서 역 직렬화 할 수도 있습니다.
경로 'allTimeSum', 1 호선, 위치 (14)
내가 기분이 모델이 필요하다고 생각하지 않습니다 시도 오류 메시지는 제공 한 정보에 따라 얻을 수있는 답변을 제공합니다. JSON 소스가 배열이 아닙니다. –
이 작업을 수행하려면 무엇을해야합니까? 나는 C# inbuilt 붙여 넣기 특별하게 사용할 수있는 붙여 넣기를 사용하여 시도했다. 나는 잃었다. –
웹 요청은 ** {**}으로 시작하는 JSON ** 객체 **를 반환하지만 JSON ** 배열 ** (** [**]으로 시작됨) (예외 메시지는 매우 명확 함) . 반환 된 JSON의 일부 데이터를 cherrypick하려고하지만 코드가 마법 작업을 완료하기에 충분하지 않은 것 같습니다. –