美好的寂己 2019-08-08 17:49 采纳率: 100%
浏览 392
已采纳

字符串解析打赏高!!!!!!!!!!

[{"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>(数据库数据);
  • 写回答

3条回答 默认 最新

  • 美好的寂己 2019-08-08 18:26
    关注

    [{"name":"直辖市","cityList":[{"CityID":"2","cityName":"上海 ","cityEname":"Shanghai"}]}]

    json字符串被包在方括号[]之中,因此是为数组,所以要添加List<>而不只是对象
    JsonConvert.DeserializeObject>();

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容