简体   繁体   English

无法将类型“Newtonsoft.Json.Linq.JObject”转换为复杂类型

[英]Cannot convert type 'Newtonsoft.Json.Linq.JObject' to Complex Type

I have json as follows,我有 json 如下,

{
  "H": "Macellum",
  "M": "Receive",
  "A": [
    {
      "CustomerId": "172600",
      "OrderId": "69931",
      "OrderStatus": "E0",
      "Buy": "A"
    }
  ]
}

and complex type和复杂类型

public class OrderStats
{
    public string CustomerId { get; set; }
    public string OrderId { get; set; }
    public string OrderStatus { get; set; }
    public string Buy { get; set; }
}

I am trying a casting as follows,我正在尝试如下铸造,

dynamic obj = JsonConvert.DeserializeObject<dynamic>(message);
OrderStats info = (OrderStats)obj.A[0]; //exception
OrderStats info = obj.A[0] as OrderStats; //info is null

But error as follows但是报错如下

Cannot convert type 'Newtonsoft.Json.Linq.JObject' to OrderStatus无法将类型“Newtonsoft.Json.Linq.JObject”转换为 OrderStatus

How about this one? 这个怎么样?

var str = "YOUR_JSON_HERE";
var obj = JsonConvert.DeserializeObject<dynamic>(str);
OrderStats info = ((JArray)obj.A)[0].ToObject<OrderStats>();

I found a solution a bit trivial like this, 我发现这样的解决方案有点琐碎,

dynamic obj = JsonConvert.DeserializeObject<dynamic>(message);
OrderStats info = JsonConvert.DeserializeObject<OrderStats>(JsonConvert.SerializeObject(obj.A[0]));

i`m reviewing this solutions, but i think it's not necessary to use dynamic key,我正在审查这个解决方案,但我认为没有必要使用动态密钥,

i did this and it's work for me:我这样做了,它对我有用:

OrderStats info = JsonConvert.DeserializeObject(str) OrderStats 信息 = JsonConvert.DeserializeObject(str)

It's simplified that other lines that you mention, but thanks for the advice, it was它简化了您提到的其他行,但感谢您的建议,它是

correct for me对我来说正确

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法隐式转换类型&#39;Newtonsoft.Json.Linq.JObject&#39; - Cannot implicitly convert type 'Newtonsoft.Json.Linq.JObject' 无法将类型'Newtonsoft.Json.Linq.JProperty'转换为'Newtonsoft.Json.Linq.JObject' - Cannot convert type 'Newtonsoft.Json.Linq.JProperty' to 'Newtonsoft.Json.Linq.JObject' &#39;Newtonsoft.Json.Linq.JObject&#39; 类型的对象无法转换为类型 - Object of type 'Newtonsoft.Json.Linq.JObject' cannot be converted to type 无法将类型“Newtonsoft.Json.Linq.JObject”隐式转换为“System.Collections.Generic.IEnumerable”<Employee> &#39; - Cannot implicitly convert type 'Newtonsoft.Json.Linq.JObject' to 'System.Collections.Generic.IEnumerable<Employee>' 导入的类型“ Newtonsoft.Json.Linq.JObject”被多次定义 - The imported type `Newtonsoft.Json.Linq.JObject' is defined multiple times 错误 CS0266 无法将类型“Newtonsoft.Json.Linq.JObject”隐式转换为“字符串”。 存在显式转换(您是否缺少演员表?) - Error CS0266 Cannot implicitly convert type 'Newtonsoft.Json.Linq.JObject' to 'string'. An explicit conversion exists (are you missing a cast?) 无法将&#39;Newtonsoft.Json.Linq.JObject&#39;转换为实际类型 - Can't cast 'Newtonsoft.Json.Linq.JObject' to actual type 不支持集合类型“Newtonsoft.Json.Linq.JObject” - he collection type 'Newtonsoft.Json.Linq.JObject' is not supported Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray' - Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray' 使用Linq将KeyValue对转换为Newtonsoft.Json.Linq.JObject - Using Linq to convert KeyValue pairs into Newtonsoft.Json.Linq.JObject
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM