J'essaie de convertir une chaîne JSON en objet C #, mais j'obtiens des valeurs nulles
Voici la chaîne JSON que j'ai
using (StreamReader r = new StreamReader(@"SamplePDF.json")) { string json = r.ReadToEnd(); List<RootObject> items = JsonConvert.DeserializeObject<List<RootObject>>(json); }
Et les classes pour JSON objet
public class JsonHelper { string top { get; set; } string left { get; set; } string width { get; set; } string height { get; set; } string text { get; set; } } public class RootObject { public string extraction_method { get; set; } public double top { get; set; } public double left { get; set; } public double width { get; set; } public double height { get; set; } public List<List<JsonHelper>> data { get; set; } public int spec_index { get; set; } }
Et ci-dessous le code est utilisé pour obtenir l'objet
[ { "extraction_method": "stream", "top": 0.0, "left": 0.0, "width": 559.0, "height": 732.2100219726562, "data": [[ { "top": 0.0, "left": 0.0, "width": 0.0, "height": 0.0, "text": "" }, { "top": 96.36, "left": 129.27, "width": 102.97900390625, "height": 6.550000190734863, "text": "Sample" }, { "top": 96.35, "left": 311.0, "width": 27.188995361328125, "height": 6.550000190734863, "text": "PT" }, { "top": 96.35, "left": 361.0, "width": 41.248992919921875, "height": 6.550000190734863, "text": "HT" }, { "top": 96.36, "left": 432.11, "width": 28.141387939453125, "height": 6.550000190734863, "text": "RT." }, { "top": 96.36, "left": 480.88, "width": 29.64898681640625, "height": 6.550000190734863, "text": "LT." }, { "top": 96.36, "left": 522.33, "width": 36.660003662109375, "height": 6.550000190734863, "text": "MT" } ], [ { "top": 727.57, "left": 75.24, "width": 14.902000427246094, "height": 4.619999885559082, "text": "Tee" }, { "top": 0.0, "left": 0.0, "width": 0.0, "height": 0.0, "text": "" }, { "top": 727.57, "left": 315.0, "width": 14.00201416015625, "height": 4.619999885559082, "text": "IO." }, { "top": 727.59, "left": 381.43, "width": 16.6820068359375, "height": 4.619999885559082, "text": "1.10" }, { "top": 727.59, "left": 434.53, "width": 25.582000732421875, "height": 4.619999885559082, "text": "30.00" }, { "top": 727.59, "left": 488.98, "width": 21.131988525390625, "height": 4.619999885559082, "text": "8.00" }, { "top": 727.59, "left": 534.53, "width": 24.469959259033203, "height": 4.619999885559082, "text": "18.00" } ]], "spec_index": 0 } ]
Mais j'obtiens, des valeurs nulles dans l'objet comme ci-dessous (ouvrir l'image dans un nouvel onglet si l'image n'est pas affichée ci-dessous),
Faites-moi savoir sur le même, merci
3 Réponses :
Vous pouvez leur donner une valeur par défaut comme:
public int X { get; set; } = 0; public string Y { get; set; } = "";
Comment cela résoudrait-il le problème?
img ne peut pas être affiché donc je me suis trompé, je pensais que la désérialisation était le problème
N'a pas rendu les propriétés ci-dessous accessibles, voici les changements. Merci à @John.
public class JsonHelper { string top { get; set; } string left { get; set; } string width { get; set; } string height { get; set; } string text { get; set; } }
Votre classe ne semble pas correspondre au format de la chaîne JSON, essayez ceci
alors JSON.Net correspondra à la chaîne Object et JSON
J'ai essayé votre exemple de chaîne JSON et l'objet est créé à l'étape 4 comme ceci
public class Rootobject { public Class1[] Property1 { get; set; } } public class Class1 { public string extraction_method { get; set; } public float top { get; set; } public float left { get; set; } public float width { get; set; } public float height { get; set; } public Datum[][] data { get; set; } public int spec_index { get; set; } } public class Datum { public float top { get; set; } public float left { get; set; } public float width { get; set; } public float height { get; set; } public string text { get; set; } }
@John Merci, les propriétés "JSONHelper" n'étaient pas accessibles.Silly Me !!!