且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法将当前json对象反序列化,因为(例如。{name":" value"}})因为类型需要json数组(例如[1,2,3])

更新时间:2021-09-15 21:58:44

以下Json

The following Json
{
  "success": 1,
  "login": [
    {
      "username": "123456",
      "password": "123456",
      "store_authority": "Yes",
      "fyear": "2018-19"
    }
  ]
}



不是一个可枚举的Root Objects。它包含一个列表但不是一个。



反序列化的正确方法是:


Is not an enumerable of "Root Objects. It contains a list but isn't one.

The correct way to deserialize it is:

var model = JsonConvert.DeserializeObject<RootObject>(json);





要成为desrializer的有效JSON,它必须如下所示:



To be valid JSON for your desrializer it would have to look like this:

[{
  "success": 1,
  "login": [
    {
      "username": "123456",
      "password": "123456",
      "store_authority": "Yes",
      "fyear": "2018-19"
    }
  ]
}]



注意额外的方括号



希望有帮助

Andy


Note the extra square brackets

Hope that helps
Andy


JsonConvert.DeserializeObject<List<RootObject>>(json);





您将json反序列化为RootObject对象数组,但是json不包含这些项的数组,只有一个,因此反序列化为RootObject,您不需要List。



You are deserialising the json into an array of RootObject objects, however the json don't contain an array of those items, only one, so deserialise to just RootObject, you don't need the List.