0

I'm trying to deserialise a json file what looks like this

[
   {
      "Name":"Diluc",
      "BaseATK":335,
      "BaseDef":784,
      "BaseHP":12981,
      "BaseCR":"24.2",
      "BaseCD":50
   },
   {
      "Name":"Amber",
      "BaseATK":223,
      "BaseDef":601,
      "BaseHP":9461,
      "BaseCR":"atk%",
      "BaseCD":50
   },
   {
      "Name":"Barbara",
      "BaseATK":159,
      "BaseDef":669,
      "BaseHP":9787,
      "BaseCR":5,
      "BaseCD":50
   },
   {
      "Name":"Beidou",
      "BaseATK":225,
      "BaseDef":648,
      "BaseHP":13050,
      "BaseCR":5,
      "BaseCD":50
   }
]

I am trying to put this information into a list to be used right now i have a class what looks like this

Public Class characterdetails
    Public name As String
    Public baseATK As Integer
    Public baseHP As Integer
    Public baseCR As Double
    Public baseCD As Double

    Public characterlist As List(Of characterdetails)

End Class

and i'm trying to put it into characterlist i'm using json.net and i'm unsure of how to do this

the file path to the json currently is D:\Coursework design\Coursework design\Resources\CharacterValuescollection.json

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
  • What exactly is your problem? Are you just not sure how to use Json.NET to deserialize a `List(Of characterdetails)` from a file? If so see the documentation page [Deserialize JSON from a file](https://www.newtonsoft.com/json/help/html/DeserializeWithJsonSerializerFromFile.htm) as well as [`JsonHelpers.CreateFromJsonFile()` from this answer](https://stackoverflow.com/a/17925665/3744182) by [Tok'](https://stackoverflow.com/users/1774251/tok) to [Can Json.NET serialize / deserialize to / from a stream?](https://stackoverflow.com/q/8157636/3744182). – dbc Jan 26 '22 at 17:18
  • Also, 1) Your JSON is malformed: the property `BaseCD` should be quoted: `"BaseCD"`. Is that just a typo in the question, or is your actual JSON malformed? 2) Why are you not trying to deserialize the property `"BaseDef"`? 3) Why are you making `characterlist` a property of `characterdetails`? From the JSON it does not appear that `characterdetails` is a recursive data structure. – dbc Jan 26 '22 at 17:21

1 Answers1

1

Visual Studio has a cool feature called Paste JSON as Classes that can be found under Edit > Paste Special > Paste JSON as Classes. If you were to do this, then you would get something that looks like the following:

Public Class CharacterDetails
    Public Property Name As String
    Public Property BaseATK As Integer
    Public Property BaseDef As Integer
    Public Property BaseHP As Integer
    Public Property BaseCR As Object
    Public Property BaseCD As Integer
End Class

With this class, you can then serialize the JSON array to a collection of CharacterDetails by using JsonConvert.DeserializeObject (documentation).

Here is an example:

Dim details = JsonConvert.DeserializeObject(Of List(Of CharacterDetails))(json)

Fiddle: https://dotnetfiddle.net/IyLkIY

David
  • 5,877
  • 3
  • 23
  • 40