【问题标题】:Cannot deserialize the current JSON array in C#无法在 C# 中反序列化当前 JSON 数组
【发布时间】:2015-01-12 19:41:52
【问题描述】:

我正在尝试从 VB.NET 切换到 C#;但是我在使用 JSON 时遇到了一些问题。我正在尝试反序列化 JSON 数组。但是,我收到此错误:

无法反序列化当前的 JSON 数组

这是我要反序列化的类:

public class ReturnObject
{
    public int id { get; set; }
    public string name { get; set; }
    public string url { get; set; }
    public bool thumbnailFinal { get; set; }
    public string thumbnailUrl { get; set; }
    public object bcOverlayUrl { get; set; }
    public object personalServerOverlayUrl { get; set; }
}

反序列化代码:

 Debug.WriteLine(JsonConvert.DeserializeObject<ReturnObject>(richTextBox1.Text));

这是我尝试检索的 JSON 的 Web URL。

http://www.roblox.com/place-thumbnails?params=[{placeId:1818}]

【问题讨论】:

  • 显示反序列化的代码
  • Debug.WriteLine(JsonConvert.DeserializeObject(richTextBox1.Text));添加到主帖

标签: c# arrays json serialization


【解决方案1】:

您试图将 JSON 数组反序列化为单个对象,但这是无法完成的。

将您的反序列化代码更改为:

var deserialized = JsonConvert.DeserializeObject<ReturnObject[]>(richTextBox1.Text)

查看结果:

foreach(var obj in deserialized)
{
    Debug.WriteLine(obj.id);
}

【讨论】:

  • 谢谢,成功了。最后一个问题假设我想检索 id 或 thumbnailUrl 我该怎么做?
  • 您将遍历结果并获取您需要的信息
  • 奇数。它正在显示 SomeApp.Form1+ReturnObject[]
  • 当然,因为那是数组的类型。我会更新代码
  • 谢谢您,对于经常出现的问题,我深表歉意。我还是个新手。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-12
  • 2017-04-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多