2017-08-17 166 views
0

我在C#新手我想反序列化在C#中JSON对象,但我得到的错误:C#JSON反序列化错误队列

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.

我读过从具有相同的其他用户的另一个解决方案问题,但我无法修复它。

我的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" 
} 

这些都是我的课:

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

    } 

而且that's我如何调用数组的

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

感谢

的一个值

回答

2

usuarios是一个数组。使用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

非常感谢,我用列表也工作。 –

+0

这是正确的做法! –

1

非常简单。看看

 "nombre": "asas, 

你忘了结束报价。使用JSON Validator以确保您的JSON在您未来检查其他任何内容之前有效。

+0

打我吧,哈哈 – DNKROZ

+0

我一直在打我自己一堆的时代,这是一个数字游戏:) – yusif

+0

是的,有与阵列也是一个问题。刚刚看到别人已经回答了这个问题 – yusif