由于有消息称,您的目标是JObject
所以不要将其转换为JArray
。试试这个:
var objs = JsonConvert.DeserializeObject("{ \"PrintId\":10,\"Header\":\"header\",\"TC\":\"tc\",\"CompanyRef\":\"00000000-0000-0000-0000-000000000000\"}");
更新为了获得一个collection List<Print>
,你的JSON必须是一个数组。试试这个(我做你的JSON数组,并添加了第二个对象):
string json = "[{ \"PrintId\":10,\"Header\":\"header\",\"TC\":\"tc\",\"CompanyRef\":\"00000000-0000-0000-0000-000000000000\"}"
+ ",{ \"PrintId\":20,\"Header\":\"header2\",\"TC\":\"tc2\",\"CompanyRef\":\"00000000-0000-0000-0000-000000000000\"}]";
var objs = JsonConvert.DeserializeObject<List<Print>>(json);
//The loop is only for testing. Replace it with your code.
foreach(Print p in objs){
Console.WriteLine("PrintId: " + p.PrintId);
Console.WriteLine("Header: " + p.Header);
Console.WriteLine("TC: " + p.TC);
Console.WriteLine("CompanyRef: " + p.CompanyRef);
Console.WriteLine("==============================");
}
public class Print
{
public int PrintId { get; set; }
public string Header { get; set; }
public string TC { get; set; }
public string CompanyRef { get; set; }
}
这里是一个fiddle。
答案很简单。它不是一个数组。它是一个对象,并且您尝试将其转换为数组。 –
* this * code没有问题 - json文本是* not *数组,它是一个字典。如果你以前认为它是有效的,那么文本是不同的或者是演员。 –
您提供的代码绝对*不*适用于相同的JSON。 –