Cannot deserialize the current JSON array (e.g. [1,2,3])

早过忘川 提交于 2021-02-05 09:46:34

问题


i've this error: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'authObject' 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.

my deserialize code

authObject a = JsonConvert.DeserializeObject<authObject>(result);

my class with json object description:

public class authObject
{

        public string auth { get; set; }
        public string type { get; set; }
        public string sess_id { get; set; }

}

Ofcourse my json :

[{"auth":"49","type":"P","sess_id":"89a0d5gle5vspo0ed3j8tbdos4"}]

what i have been done wrongly ? Thanks


回答1:


It requires an array or a list, try

List<authObject>  list = JsonConvert.DeserializeObject<List<authObject>>(result);


来源:https://stackoverflow.com/questions/22768013/cannot-deserialize-the-current-json-array-e-g-1-2-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!