Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Forms:JsonConvert.DeserializeObject始终返回null_C#_Xamarin.forms_Json.net_Xamarin Live Player - Fatal编程技术网

C# Forms:JsonConvert.DeserializeObject始终返回null

C# Forms:JsonConvert.DeserializeObject始终返回null,c#,xamarin.forms,json.net,xamarin-live-player,C#,Xamarin.forms,Json.net,Xamarin Live Player,我尝试使用Newtonsoft.json将json字符串反序列化为对象。但是JsonConvert.DeserializeObject总是返回null string json2 = "[{ 'id':1,'date':'2016-05-10T03:48:21','date_gmt':'2016-05-10T03:48:21','guid':{ 'rendered':'http://test.de/?p=1'},'modified':'2019-02-27T11:56:21'}]"; List&

我尝试使用Newtonsoft.json将json字符串反序列化为对象。但是JsonConvert.DeserializeObject总是返回null

string json2 = "[{ 'id':1,'date':'2016-05-10T03:48:21','date_gmt':'2016-05-10T03:48:21','guid':{ 'rendered':'http://test.de/?p=1'},'modified':'2019-02-27T11:56:21'}]";

List<Product> myProducts = new List<Product>();

myProducts = JsonConvert.DeserializeObject<List<Product>>(json2); // allways null!?
有人能帮我吗

但是JsonConvert.DeserializeObject总是返回null

string json2 = "[{ 'id':1,'date':'2016-05-10T03:48:21','date_gmt':'2016-05-10T03:48:21','guid':{ 'rendered':'http://test.de/?p=1'},'modified':'2019-02-27T11:56:21'}]";

List<Product> myProducts = new List<Product>();

myProducts = JsonConvert.DeserializeObject<List<Product>>(json2); // allways null!?
您的代码没有问题。通常这会返回一个json数组对象

如果您直接检查myProducts,那么它是一个数组对象,您需要指定数组中的哪个元素,哪个属性可以被渲染

由于json数组只包含一个对象,因此可以按如下方式编写渲染:

myProducts[0].MyGuid.Rendered
然后返回:

http://test.de/?p=1
所有参数如下所示:

myProducts//------ System.Collections.Generic.List`1[App1.Views.MainPage+Product]
myProducts[0].Id //------1
myProducts[0].Date//------2016-05-10T03:48:21
myProducts[0].Date_gmt//------2016-05-10T03:48:21
myProducts[0].Modified//------2019-02-27T11:56:21
myProducts[0].MyGuid.Rendered//------http://test.de/?p=1

如果也有问题,您可以共享解决方案的链接。我会检查它。

首先,单引号在JSON中不是有效的分隔符,尽管我不知道JSON.net是否严格执行了这一点。您的代码在我这方面运行良好。我设法反序列化了一个产品对象,其所有属性都不为null。我建议你试试另一条路。创建一个具有相应属性的产品实例,尝试将其序列化,并将结果与初始JSON字符串进行比较。很好luckAre你在用xamarin live播放器吗?如果是这样,Json.NET序列化程序显然无法在那里工作,请参见和。您的代码没有问题。如果您直接检查myProducts,则它是一个数组对象,您需要指定数组中的哪个元素,哪个属性可以被呈现。由于您的Json数组只包含一个对象,因此呈现可以这样编写:myProducts[0].MyGuid.Rendered。然后将返回:http://test.de/?p=1.@dbc:谢谢你的回答和链接。是的,我正在使用xamarin live player进行Android项目。在一个普通的WPF项目中,代码运行良好,。。。代码运行良好。我认为问题实际上在于xamarin live player与json.NET的结合。@是的,这可能是原因。您可以共享有关示例项目的链接。