简体   繁体   English

C#JSON反序列化错误数组

[英]C# JSON Deserialization Error Array

I'm newbie in C# I want to Deserialize a JSON object in C# but I'm getting error: 我是C#的新手,我想在C#中反序列化JSON对象,但出现错误:

We had a problem: Cannot deserialize the current JSON array (eg [1,2,3]) into type 'ZK4000_Example.JsonParser+user' because the type requires a JSON object (eg {"name":"value"}) to deserialize correctly. 我们遇到了一个问题:无法将当前JSON数组(例如[1,2,3])反序列化为类型'ZK4000_Example.JsonParser + user',因为该类型需要一个JSON对象(例如{“ name”:“ value”})正确反序列化。 To fix this error either change the JSON to a JSON object (eg {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (eg ICollection, IList) like List that can be deserialized from a JSON array. 要解决此错误,可以将JSON更改为JSON对象(例如{“ name”:“ value”}),也可以将反序列化类型更改为数组,或者将实现集合接口的类型(例如ICollection,IList)更改为List,例如List从JSON数组反序列化。 JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. 还可以将JsonArrayAttribute添加到类型中,以强制其从JSON数组反序列化。 Path 'usuarios', line 1, position 24. 路径“ usuarios”,第1行,位置24。

Path '', line 1, position 1. 路径'',第1行,位置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 谢谢

usuarios is an array. usuarios是一个数组。 use user[] 使用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; }
    }

}

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. 使用JSON验证程序来确保您的JSON有效,然后再检查其他内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM