Eduardo Fenix
Eduardo Fenix

Reputation: 3

C# JSON Deserialization Error Array

I'm newbie in C# I want to Deserialize a JSON object in C# but I'm getting error:

We had a problem: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ZK4000_Example.JsonParser+user' 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 'usuarios', line 1, position 24.

Path '', line 1, position 1.

I've read another solutions from other users that have the same issue,but I haven´t be able to fix it.

My Json:

{
  "estado": 1,
  "usuarios": [
    {
      "nombre": "as",
      "id_usuario": "34",
      "huella_string": "1"
    },
    {
      "nombre": "ded",
      "id_usuario": "35",
      "huella_string": "1"
    },
    {
      "nombre": "sa",
      "id_usuario": "36",
      "huella_string": "1"
    },
    {
      "nombre": "xz",
      "id_usuario": "12",
      "huella_string": "1"
    },
    {
      "nombre": "asas,
      "id_usuario": "28",
      "huella_string": "1"
    },
    {
      "nombre": "asscass",
      "id_usuario": "7",
      "huella_string": "mspZVoOalsE9QQsrwkQBBSS/PoEMo8BCAQmiLS/BC5YuKYEVicJIQQkTQlFBCxDES4EPFbE+wQI0UkqBDYW5KYEIKEs6QQmaTzYBEZEjGQEV7qxYAQaczhfBDeQaTEEGH8M0AQelrk0BDCVMK4EOlk8owRGHLTwBBqM8EUEd3CgQwQ/dCUSBAxrEJsEJpcgcATU3NT4BBK5ECgEEzpcJwQXloBIBD2UOIkELfJM4AQmKyFcBDI8QV8EDlU9ZAQ2GKl6BBhlWxJAFXdCyBwANWFxldQwbJCgpJiMjIiMADVZZZXYPICgsLCgkIiIjAAxcX2Z2DBghJSYlIyIiAA1WWWZ0FCMqLS0nIyMiJAAMYWNpdQkTHCEjIiAgHwANVltmAholKi0sJiMiIiUADGhpb3cHEBkdICAdHBwADFZcag0hKCssKSUhISEADGlscXcHDxYbHR0bGxoADFRYYBkkKiopJSIdHB0ADGtucncGDhUaHBsaGhsADVRXTCsoKigmIyAcGh0pAAxsb3MBCA8WGhsbGhscAAxUVU42KCYlIyEdGhccAAtwcnUCCRAWGhsaGhsADFdWUz0kIiIhIBwaFx0BC3V3BAkRFhkbGxscAAtXV1dfGhobGhoWEgwBCnYBBAkPFRobGxsADGxgXm4KERISEA0LCgsDCQQIDxUZHBwBC3FkbwULDQwLCAYE"
    }
  ],
  "peticion": "seleccion_usuarios"
}

These are my classes:

class JsonParser
    {
        public int estado { set; get; }
        public string peticion { set; get; }
        public user usuarios { set; get; }

        public  class user
        {
            public string id_usuario { set; get; }
            public string huella_string { set; get; }
            public string nombre { set; get; }
        }

    }

And that´s how I call one of the values of the array

var Json = JsonConvert.DeserializeObject<JsonParser>(strJSON);
ShowHintInfo(Json.usuarios.id_usuario);

Thanks

Upvotes: 0

Views: 1042

Answers (2)

Orkhan Alikhanov
Orkhan Alikhanov

Reputation: 10050

usuarios is an array. use user[]

class JsonParser
{
    public int estado { set; get; }
    public string peticion { set; get; }
    public user[] usuarios { set; get; }

    public  class user
    {
        public string id_usuario { set; get; }
        public string huella_string { set; get; }
        public string nombre { set; get; }
    }

}

Upvotes: 2

yusif
yusif

Reputation: 193

Very simple. Look at

      "nombre": "asas,

You forgot the closing quote. Use a JSON Validator to make sure your JSON is valid before you check anything else in the future.

Upvotes: 1

Related Questions