0

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

    2 Answers 2

    2

    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; } } } 
    0
      1

      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.

      5
      • Beat me to it haha
        – DNKROZ
        CommentedAug 17, 2017 at 17:05
      • I've been beaten myself a bunch of times, it's a numbers game :)
        – yusif
        CommentedAug 17, 2017 at 17:07
      • Yes, there's an issue with the array as well. Just saw that someone else answered it already
        – yusif
        CommentedAug 17, 2017 at 17:08
      • 2
        That may be a problem with his JSON but is not the reason for the error message, so even if he fixes this, he won't get rid of the error.CommentedAug 17, 2017 at 17:08
      • thanks, although that was a typo when I posted the question,I changed the parameters because there were real names.CommentedAug 17, 2017 at 18:51

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.