【问题标题】:Trying to Deserialise JSON for .NET from a URL, but getting an error尝试从 URL 反序列化 .NET 的 JSON,但出现错误
【发布时间】:2014-03-01 21:08:17
【问题描述】:

我正在尝试使用以下代码反序列化 URL:

     private void RefeshList_Click(object sender, EventArgs e)
        {
            WebClient list = new WebClient();
            string text = list.DownloadString("http://www.classicube.net/api/serverlist/");
            var server = JsonConvert.DeserializeObject<RootObject>(text);
             serverlist.Text = text;

            }
        }
    }

public class RootObject
{
    public string hash { get; set; }
    public string ip { get; set; }
    public int maxplayers { get; set; }
    public string mppass { get; set; }
    public string name { get; set; }
    public int players { get; set; }
    public int port { get; set; }
    public int uptime { get; set; }

}

但我得到一个错误:

An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll

Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'RootObject' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.

Path '', line 1, position 1.

网址是:http://www.classicube.net/api/serverlist/

【问题讨论】:

  • 错误信息告诉你问题出在哪里——只是说':)

标签: c# json


【解决方案1】:

它是一个 json 数组。使用

var server = JsonConvert.DeserializeObject<List<RootObject>>(text);

【讨论】:

    【解决方案2】:

    除了列出的正确答案之外,我还想更进一步。如果您检查从服务器返回的 JSON 响应,您将看到:

    [
        {"hash": "84ce710714eedcdfd7ef22d4776671b0", "maxplayers": 64, "name": "All in the Mined", "players": 0, "uptime": 2107198},
    

    开头的[ 表示JSON 语法中的数组。很多时候,人们可能会看到包含数组属性的 JSON 对象;但是,这里不是这种情况。这意味着您的代码需要返回一个数组(并且大多数序列化程序都明白数组是 IEnumerable,因此它们对任何 IEnumerable 都很满意)。

    如果你仔细想想,这很有意义。您会在 JSON 响应中看到大约 30 个这些对象 - 您需要一个适当的数据结构来包含所有返回的对象。单个对象不匹配。

    因此,您需要稍微更改您的代码,将List&lt;&gt; 添加到其中。这是这里唯一需要的更改。

    var server = JsonConvert.DeserializeObject&lt;List&lt;RootObject&gt;&gt;(text);

    【讨论】:

      【解决方案3】:

      您必须使用List&lt;RootObject&gt; 正确反序列化它:

      List<RootObject> server = JsonConvert.DeserializeObject<List<RootObject>>(text);
      

      【讨论】:

      • @WWasif Hossain 你总是做同样的事情。 stackoverflow.com/questions/22020684/…
      • @L.B 我想消除您的困惑,即它只是重叠事件。当我看到尚未接受任何答案时,我试图立即回答。我不知道无意中将答案与某人匹配是否是错误的。
      • 你认为这是一种好的态度吗?与其发布相同的答案,不如投票给正确的答案。 (我的意思不是几分钟的差异,我说的是半小时后。)
      • 我总是尝试为适当的答案投票,并鼓励其他人这样做。好的,那么我会尝试先阅读所有答案,然后相应地进行下一步。谢谢!
      【解决方案4】:
      "hash": "84ce710714eedcdfd7ef22d4776671b0"
      "maxplayers": 64
      "name": "All in the Mined"
      "players": 0
      "uptime": 2106398
      

      你需要这样设置你的类

      public class RootObject
      {
          public string hash { get; set; }
          public int maxplayers { get; set; }
          public string name { get; set; }
          public int players { get; set; }
          public int uptime { get; set; }
      
      }
      

      【讨论】:

      • 这不是问题。问题是从服务器返回的文本描述了一个集合,正如 [] 所证明的那样。
      猜你喜欢
      • 2022-01-08
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多