反序列化 JSON 数组

Deserialize a JSON array

我正在尝试使用 Newtonsoft 命名空间在 C# 中反序列化 JSON 数据。

这是我的类:

class lastResponse
{
    public string type { get; set; }
    public Metadata metadata { get; set; }
    // public string[] course { get; set; }
    public List<object> course { get; set; }
    public string publisher { get; set; }
}

public class Metadata
{
    public string bookID { get; set; }
    public string title { get; set; }
    public string filename { get; set; }
}

此代码:

var errorMsg = JsonConvert.DeserializeObject<lastResponse>(downloader.LastResponse);

给我这个错误:

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 'BookManager.lastResponse' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

请帮我弄清楚我在这里遗漏了什么。

最可能的原因可能是您从 downloader.LastResponse 获得的 json 不是数组。

你的Json可能是这样的。

{
    "type": "First",
    "metadata": {
        "bookID": "book123",
        "title": "bookTitle Here",
        "filename": "book file name"
    },
    "course": [
        {
            "1": "01",
            "2": "02"
        },
        {
            "001": "001",
            "002": "002"
        }
    ],
    "publisher": "you"
}

您需要更改的是在 downloader.LastResponse 中传递 JSON 数组,或者您可以按如下方式更改反序列化对象语句。

JsonConvert.DeserializeObject<lastResponse>(downloader.LastResponse);

根据你的操作,正确的JSON应该是这样的。

**[**
    {
        "type": "First",
        "metadata": {
            "bookID": "book123",
            "title": "bookTitle Here",
            "filename": "book file name"
        },
        "course": [
            {
                "1": "01",
                "2": "02"
            },
            {
                "001": "001",
                "002": "002"
            }
        ],
        "publisher": "you"
    }
**]**

这可能会解决您的问题:)

你可以使用javascriptserializer

string s = "YouJsonText";
var serializer = new JavaScriptSerializer();
var result = serializer.Deserialize(s);
//or
YouCustomClass res = serializer.Deserialize<YouCustomClass>(sb.ToString());

此外,您可以像这样使用 CustomJsonConverter:

public class YouCustomClassConverter : JavaScriptConverter
{
  public override object Deserialize(IDictionary<string, object> dictionary, Type type,                      JavaScriptSerializer serializer)
  {
      throw new NotImplementedException();
  }

  public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
  {
      throw new NotImplementedException();
  }

   //and first you need register type, which you want Deserialize
   public override IEnumerable<Type> SupportedTypes
   {
      get { return new[] { typeof(YouCustomClass ) }; }
   }

}

//and then example of using JavaScriptSerializer with custom converter
var ser = new JavaScriptSerializer();
ser.RegisterConverters(new JavaScriptConverter[] { new YouCustomClassConverter() });
try
{
     YouCustomClass obj = ser.Deserialize(jsonString);
}

注意:您需要使用using System.Web.Script.Serialization;