[{"name":"直辖市","cityList":[{"CityID":"2","cityName":"上海 ","cityEname":"Shanghai"}]}]
怎么转化成对象
我在解析的时候无线报错
:annot deserialize the current JSON array (e.g. [1,2,3]) into type 'ConsoleApplication5.Root' because the type requires a JSON object (e.g. {\"name\":\"value\"}) to deserialize correctly.\r\nTo 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.\r\nPath '', line 1, position 1
public class CityListItem
{
public int CityID { get; set; }
public string cityName { get; set; }
public string cityEname { get; set; }
}
public class PolicyCity
{
public string name { get; set; }
public List<CityListItem> cityList { get; set; }
}
public class PolicyRoot
{
public PolicyCity PolicyCity { get; set; }
}
数据库数据= [{"name":"直辖市","cityList":[{"CityID":"2","cityName":"上海 ","cityEname":"Shanghai"}]}]
var city = JsonConvert.DeserializeObject<PolicyRoot>(数据库数据);
[{"name":"直辖市","cityList":[{"CityID":"2","cityName":"上海 ","cityEname":"Shanghai"}]}]
json字符串被包在方括号[]之中,因此是为数组,所以要添加List<>而不只是对象
JsonConvert.DeserializeObject>();
可以参考这边文章:https://www.cnblogs.com/echolxl/p/3594112.html
请发一下处理json的代码
一个不能将将当前JSON数组(例如[1,2,3])反序列化为类型'ConsoleApplication5.Root',因为该类型需要JSON对象(例如{\“name \”:\“value \”})才能正确反序列化 。\ r \ n要修复此错误,请将JSON更改为JSON对象(例如{\“name \”:\“value \”})或将反序列化类型更改为数组或实现集合接口的类型(例如 ICollection,IList)就像可以从JSON数组反序列化的List。 JsonArrayAttribute也可以添加到类型中以强制它从JSON数组反序列化。\ r \ nPath'',第1行,第1位