[英]"Cannot deserialize the current JSON object into type
So I have the following task - I need to deserialize an array of Jason objects which is stored in a recourse folder in my application. 因此,我有以下任务-我需要反序列化存储在应用程序的资源文件夹中的Jason对象数组。
So far I have accessed the recourse and stored it into a var: 到目前为止,我已经访问了资源并将其存储到var中:
var jsonData = Resources.SamplePoints;
Converted the array into a string: 将数组转换为字符串:
string jsonObjts = Encoding.Default.GetString(jsonData);
And have tried to write the result into a list of Dictionaries 并尝试将结果写入字典列表
List<Dictionary<string, double>> EntityData = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Dictionary<string, double>>>(jsonObjts);
However the above error message shows up when I run and test the application. 但是,当我运行和测试该应用程序时,会出现上述错误消息。
Can anyone please set me on the right path how to fix this? 谁能为我设定正确的解决方法?
A sample of the objects in question: 有关对象的样本:
"samples": [
{
"date": "2014-08-10T09:00:00Z",
"temperature": 10,
"pH": 4,
"phosphate": 4,
"chloride": 4,
"nitrate": 10
},
{
"date": "2014-08-12T09:05:00Z",
"temperature": 10.5,
"pH": 5,
"chloride": 4,
"phosphate": 4
},
Is there a reason you don't want to create a class to represent your JSON? 您是否有不想创建代表JSON的类的原因?
public class Rootobject
{
public List<Sample> Samples { get; set; }
}
public class Sample
{
public DateTime Date { get; set; }
public float Temperature { get; set; }
public int Ph { get; set; }
public int Phosphate { get; set; }
public int Chloride { get; set; }
public int Nitrate { get; set; }
}
then you can deserialize: 那么您可以反序列化:
var obj = JsonConvert.DeserializeObject<Rootobject>(json);
If you still want to use a Dictionary, I think you need something like this: 如果您仍然想使用字典,我认为您需要这样的东西:
var obj = JsonConvert.DeserializeObject<Dictionary<string, List<Dictionary<string, object>>>>(json);
Your schema in generic type List<Dictionary<string, double>>
and schema of json does not match. 通用类型为List<Dictionary<string, double>>
模式和json的模式不匹配。 If you want to cast to List<Dictionary
you can't use "samples": [
For this you'll need Dictionary<string,List<Dictionary
. 如果要转换为List<Dictionary
,则不能使用"samples": [
为此,您需要Dictionary<string,List<Dictionary
。 Also when you are trying to use double
, date won't get parsed to double. 另外,当您尝试使用double
,date不会解析为double。 So maybe you can try Dictionary<string, List<Dictionary<string, object>>>
. 因此,也许您可以尝试使用Dictionary<string, List<Dictionary<string, object>>>
。 Ideally you should create class for this structure instead of using generic List, Dictionary structure.If you are using visual studio, you can create these classes by using Edit->Paste special-> paste json as classes 理想情况下,您应该为此结构创建类,而不是使用通用的List,Dictionary结构。如果您使用的是Visual Studio,则可以通过使用Edit-> Paste special-> paste json作为类来创建这些类
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.