简体   繁体   English

在C#中反序列化JSON时出现问题-“无法反序列化当前JSON对象”

[英]Problem deserializing json in C# - “Cannot deserialize the current JSON object”

I am trying to deserialize json and clearly it's not going well since I'm here. 我正在尝试反序列化json,因为我在这里,显然它运行不顺利。 I'm not only looking for help to make it work but also some link where it's explained how it works because I couldn't find one and I want to learn. 我不仅在寻求帮助以使其运行,而且还在一些链接中对它的工作方式进行了解释,因为我找不到一个并且想学习。

var json = w.DownloadString(url);

var data = JsonConvert.DeserializeObject<Rates[]>(json);

Json looks like this: 杰森看起来像这样:

 {
   "info":"text",
   "sub":"text",
   "data":[
      {
         "date":20181111,
         "exchange":"New York",
         "open":1000.43,
         "high":1239.91,
         "low":1231.41
      },
      {
         "date":20181111,
         "exchange":"New York",
         "open":1000.43,
         "high":1239.91,
         "low":1231.41
      }
   ]
}

I have these classes: 我有这些课:

    public class Rates
    {
        public List<Data> data { get; set; }
    }

    public class Data
    {
        public int date { get; set; }
        public string exchange { get; set; }
        public double open { get; set; }
        public double high { get; set; }
        public double low { get; set; }
    }

    public class RootObject
    {
        public string info { get; set; }
        public string sub { get; set; }
        public List<Data> data { get; set; }
    }
}

There's two issues. 有两个问题。

First is that your JSON is invalid (at least in the question itself).. you're missing an array closing bracket but I suspect it's a typo. 首先是您的JSON无效(至少在问题本身中)..您缺少数组右括号,但我怀疑这是一个错字。

Other than that you want to deserialize into Rates class not an array of Rates. 除此之外,您还想反序列化到Rates类,而不是Rates数组。

var data = JsonConvert.DeserializeObject<Rates>(json);


To Enumerate through the Rates.. use the following: 要枚举Rates ..,请使用以下命令:

foreach(Data rate in data.data){

    Console.WriteLine(rate.exchange);

}

Check out this dotnetfiddle. 看看这个dotnetfiddle。

Your json string returned represents the root object. 返回的json字符串表示根对象。 Your classes look fine, you just deserialized into the wrong object. 您的类看起来不错,您只是反序列化为错误的对象。 The Rates object seems unnecessary, unless you don't care about the 'info', and 'sub' fields. 除非您不关心“ info”和“ sub”字段,否则Rates对象似乎是不必要的。 In that case you could deserialize into a Rates object. 在这种情况下,您可以反序列化为Rates对象。 The extra properties would simply be ignored by the serializer. 额外的属性只会被序列化程序忽略。

You will want to deserialize the json into the root object. 您将需要将json反序列化为根对象。

var root = JsonConvert.DeserializeObject<RootObject>(json);

Then you can loop through the data objects like so: 然后,您可以像这样遍历数据对象:

foreach (Data dataItem in root.data)
{
    // Do something with data
}

Just as a sidenote, a Rates[] would have looked somewhat like this in json format: 就像一个旁注一样, Rates[]在json格式下看起来会像这样:

[
  {
    "data": [
      {
         "date":20181111,
         "exchange":"New York",
         "open":1000.43,
         "high":1239.91,
         "low":1231.41
      },
      {
         "date":20181111,
         "exchange":"New York",
         "open":1000.43,
         "high":1239.91,
         "low":1231.41
      }
    ]
  },
  {
    "data": [
      {
         "date":20181111,
         "exchange":"New York",
         "open":1000.43,
         "high":1239.91,
         "low":1231.41
      },
      {
         "date":20181111,
         "exchange":"New York",
         "open":1000.43,
         "high":1239.91,
         "low":1231.41
      }
    ]
  }
]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM