无法将类型为"Newtonsoft.Json.Linq.JObject"的对象转换为类型为"Newtonsoft.Json.Linq.JArray"的对象 [英] Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray'
本文介绍了无法将类型为"Newtonsoft.Json.Linq.JObject"的对象转换为类型为"Newtonsoft.Json.Linq.JArray"的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在测试我的Web API.模拟数据我有这个:
I am testing my Web API. Mocking the data I have this:
var objs = ((JArray)JsonConvert.DeserializeObject("{ \"PrintId\":10,\"Header\":\"header\",\"TC\":\"tc\",\"CompanyRef\":\"00000000-0000-0000-0000-000000000000\"}")).Values<JObject>();
哪个给我错误:
无法将类型为"Newtonsoft.Json.Linq.JObject"的对象转换为类型为"Newtonsoft.Json.Linq.JArray"
Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray'
问题是它正在工作.我一定已经改变了一些东西,但是我不知道是什么.
The thing is it was working. I must have changed something, but I don't know what.
我的意图是将此JSON对象转换为名为Print
的.NET对象列表,该列表具有以下字段:
My intent is to convert this JSON object to a list of .NET objects called Print
which has the fields:
PrintId
Header
TX
CompnayRef
推荐答案
只需创建一个类并将其反序列化即可.
Just make a class and deserialize it.
public class Print
{
public int PrintId { get; set; }
public string Header { get; set; }
public string TC { get; set; }
public string CompanyRef { get; set; }
}
Print printObj = JsonConvert.DeserializeObject<Print>(yourJson);
printObj.PrintId = //...
这篇关于无法将类型为"Newtonsoft.Json.Linq.JObject"的对象转换为类型为"Newtonsoft.Json.Linq.JArray"的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文