Deserialize empty json propertyname

為{幸葍}努か 提交于 2019-12-13 07:03:46

问题


I have a json object coming from a web api which looks something like this:

{"":[{"id":1, "name":"name1"}, {"id":2, "name":"name2"}]}

and I have corresponding C# class for deserialize:

public class Person
{
    public int id { get; set; }
    public string name { get; set; }
}

public class RootObject
{
    public List<Person> Persons { get; set; }
}

but whenever I deserialize using Json.NET the Persons property in RootObject class is always null.

var c = JsonConvert.DeserializeObject<RootObject>(response);

I know the problem is very trivial, I really appreciate if anyone can help me out to fix this up.


回答1:


I think the Json string is flawed, if you change

{"":[{"id":1, "name":"name1"}, {"id":2, "name":"name2"}]}

to

{"Persons":[{"id":1, "name":"name1"}, {"id":2, "name":"name2"}]}

it deserializes just fine.



来源:https://stackoverflow.com/questions/14169350/deserialize-empty-json-propertyname

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