Unable to cast object of type ‘Newtonsoft.Json.Linq.JArray‘ to type ‘Newtonsoft.Json.Linq.JObject‘.

原始数据:

string test=“[{\"value\":1,\"text\":\"1\"},{\"value\":2,\"text\":\"2\"}]”

错误调用:

 JObject  jsonStr = (JObject)JsonConvert.DeserializeObject(test)

引发报错:

Unable to cast object of type ‘Newtonsoft.Json.Linq.JArray‘ to type ‘Newtonsoft.Json.Linq.JObject‘.
 public class dict
    { 
    
        /// 
        /// 字典id
        /// 
        public int value { get; set; }

        /// 
        /// 字典值
        /// 
        public string text { get; set; }

    }

解决方案一:

   var testJArray = JArray.Parse(test);
   var  testJson = testJArray .ToObject>();

解决方案二:

  var testJson= JsonConvert.DeserializeObject>(test);

你可能感兴趣的:(C#,#,Newtonsoft,Newtonsoft)