-1

So I was looking through this example to try to desialize for the first time ever https://www.newtonsoft.com/json/help/html/DeserializeObject.htm I'm trying to create objects out of the json data and then get the corresponding properties.

And well.. I got an error saying this

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'json_deserializing.OppedAccounts' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.'

I am new to json & deserializing so I am not sure what is going on here. My question is, how do I deserialize json in a propper way? I read something about lists and stuff but I couldnt connect the dots.

[
  {
    "uuid": "98e99e7a-df48-4b8b-adc9-e65c32410247",
    "name": "UsernameOne",
    "level": 4,
    "bypassesPlayerLimit": false
  },
  {
    "uuid": "b87e1cbc-c67c-4026-a359-8652ad9de8b4",
    "name": "UsernameTwo",
    "level": 4,
    "bypassesPlayerLimit": false
  }
]

CSharp Code

public partial class MainWindow : Window
    {
        public string line = null;

        public MainWindow()
        {
            InitializeComponent();
            Deserialize();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if(line != null)
            {
                OppedAccounts account = JsonConvert.DeserializeObject<OppedAccounts>(line);
                Debug.Print(account.name);
            }
        }

        private void Deserialize()
        {
            using (StreamReader sr = new StreamReader("ops.json"))
            {
                line = sr.ReadToEnd();
            }
            tbjson.AppendText(line);
        }
    }

And the class

class OppedAccounts
    {
        public string uuid { get; set; }
        public string name { get; set; }
        public int level { get; set; }
        public bool bypassplayerlimit { get; set; }
    }
Jordan Jones
  • 125
  • 10
  • 2
    `List accounts = JsonConvert.DeserializeObject>(line);` – Chetan Nov 09 '17 at 15:04
  • Possible duplicate of [Deserialize JSON into C# dynamic object?](https://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object) – xxbbcc Nov 09 '17 at 15:05

1 Answers1

5

You are deserializing an array, so you are expecting an array of objects to be returned. Change the type of account and the generic parameter of JsonConvert.DeserializeObject to List<OppedAccounts>.

List<OppedAccounts> accounts = JsonConvert.DeserializeObject<List<OppedAccounts>>(line);
Nkosi
  • 235,767
  • 35
  • 427
  • 472
Jerodev
  • 32,252
  • 11
  • 87
  • 108