2017-11-26 8 views
0

Ich bin sehr neu in C# und Visualstudio2017 und bin seit buchstäblich Wochen darauf festgefahren. Das Netz suchen, aber keine Ergebnisse finden, die ich richtig verstehen kann. Was ich versuche zu tun ist, JSON-Daten von https://zkillboard.com/api/stats/characterID/224802743/ dann in nutzbare Daten konvertieren, die ich bekommen und bestimmte Daten in bestimmten Textfeldern anzeigen kann. Ich benutzte https://quicktype.io/, um den JSON in C# -Array umzuwandeln.JsonSerializationException: Das aktuelle JSON-Objekt kann nicht deserialisiert werden

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

Hier ist die Fehlermeldung, die ich von VS2017 immer bin.

JsonSerializationException: Kann nicht das aktuelle Objekt JSON Deserialisieren (zB { "name": "value"}) in type 'ZKILLBOARDDATA.GettingStarted []', weil der Typ erfordert eine JSON Array (zB [1,2,3 ]) um richtig zu deserialisieren.

Um diesen Fehler zu beheben, ändern Sie entweder den JSON in ein JSON-Array (zB [1,2,3]) oder ändern Sie den deserialisierten Typ, so dass es ein normaler .NET-Typ ist (zB kein primitiver Typ wie Integer, nicht ein Sammlertyp wie ein Array oder eine Liste), der aus einem JSON-Objekt deserialisiert werden kann. JsonObjectAttribute kann auch zum Typ hinzugefügt werden, um die Deserialisierung von einem JSON-Objekt zu erzwingen.

Pfad 'allTimeSum', Zeile 1, Position 14.

+0

Ich fühle mich wie Die Fehlermeldung gibt Ihnen eine gute Antwort, wie Sie bekommen können, angesichts der Informationen, die Sie zur Verfügung gestellt haben. Ihre JSON-Quelle ist anscheinend kein Array. –

+0

Was muss ich tun, damit das funktioniert? Ich habe versucht, mit C# eingebaute Paste spezielle, die nicht funktioniert. Ich bin verloren. –

+0

Die Webanforderung gibt ein JSON ** -Objekt ** (beginnend mit ** {**) zurück, aber Ihr Deserialisierungsaufruf erwartete ein JSON ** -Array ** (beginnend mit ** [**) (die Ausnahmebedingungsnachricht ist sehr klar) . Scheint, du möchtest einige Daten aus dem zurückgegebenen JSON auswählen, aber dein Code hat nicht genug magisches Pulver, um diese Aufgabe abzuschließen. –

Antwort

0

Versuchen Sie, diese

 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); 

Ich glaube nicht, dass Sie dies in Ihrem Modell benötigen

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, 
     }; 
    } 
+0

Sie können die Daten wie obj.someProperty verwenden und im Textbereich festlegen. Zum Beispiel: textbox1.setText oder textbox1.innerHtml = obj.someProepty; – Hey24sheep

+0

dieser Code, den Sie empfohlen, ich entfernen wurde von https://quicktype.io generiert und so war die Klassen und Jsonobjects. Ich habe es in meinem Code und ersetzt den Code, den Sie mich gebeten, zu versuchen, und ich bekomme diesen Fehler nicht mehr, aber ich bekomme jetzt. (Diese Methode fehlt 'erwarten' Operatoren und wird synchron ausgeführt.) aber wie gehe ich über bestimmte Daten, die ich in bestimmten Textboxen etc. setzen setzen –

+0

es ist eine Windows-Formular-Anwendung nicht WPF BTW. innerHtml nicht erkannt. keiner ist setText. Ich habe auch versucht OutputBox.Text = obj.Zkb.Punkte; (aber sein zurückkehrender Fehler, kann Typ 'lang' nicht zu 'Zeichenkette' implizit konvertieren) –

Verwandte Themen