反序列化JSON数组

时间:2015-02-06 10:30:48

标签: c# json deserialization

我正在尝试使用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);

给我这个错误:

  

类型&#39; Newtonsoft.Json.JsonSerializationException的未处理异常&#39;发生在Newtonsoft.Json.dll中附加信息:无法将当前JSON数组(例如[1,2,3])反序列化为类型&#39; BookManager.lastResponse&#39;因为该类型需要一个JSON对象(例如{&#34; name&#34;:&#34; value&#34;})才能正确反序列化。

请帮我弄清楚我在这里失踪了什么。

2 个答案:

答案 0 :(得分:0)

最可能的原因可能是你从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数组,或者您可以更改deserialize对象语句,如下所示。

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"
    }
**]**

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

答案 1 :(得分:0)

您可以使用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;