美好的寂己 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条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。