0

I really need help creating classes for this JSON, I need to eventually loop through and add each name (example: 20th Anniversary Pack 1st Wave) and its corresponding code (example: 20AP) to a database, but to do that I need to deserialize this first and I cant work out how to create the classes, usually I generate them but when I tried that it did not work, tia

Here is part of my JSON string:

{
    "status": "success",
    "setsAbbreviations": {
        "20th Anniversary Pack 1st Wave": ["20AP"],
        "20th Anniversary Pack 2nd Wave": ["20AP"],
        "Beginner's Edition 1 (2011)": ["BE01"],
        "Beginner's Edition 2 (2011)": ["BE02"],
        "Booster Chronicle": ["BC"],
        "Booster R2": ["B2"],
    }
}

This is the code I have tried:

Public Class root3
Public Property status As String
Public Property setsAbbreviations As String()
End Class

And this is the error: 'An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll

Additional information: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.String[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

Path 'setsAbbreviations['20th Anniversary Pack 1st Wave']', line 1, position 74.'

9
  • Questions asking for help with code need to include the code and what exactly the problem is. it did not work is a feeble problem description. As is, this is just asking for someone to write code for you and thats not what the site is about. Commented Jan 21, 2018 at 17:00
  • @Plutonix updated, don't really know what to write though. Commented Jan 21, 2018 at 17:06
  • Thats pretty weird JSON - is it something you created or get from somewhere? [ ... ] means an array, but all those codes are just an array of 1; also the 20th ... entry has the same code for 1 and 2nd wave. The definition for setsAbbreviations is not correct to start with - VS and none of the other class creating robots made that - I'd change the definition to a Dictionary(of String, String()) Commented Jan 21, 2018 at 17:14
  • The JSON is from an API, its the only API I could find that listed both sets and codes in one go. Commented Jan 21, 2018 at 17:30
  • And know it wasnt a robot that did that, It was my terrible attempt, when the class creation tool did it, it was about 50 pages long and didn't work xD Commented Jan 21, 2018 at 17:31

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.