2015-02-06 63 views
0

请帮助我,我是新的C#和JSON工作。我想使用newtonsoft命名空间反序列化C#中的json数据。反序列化数组json c#

这里是我的课:

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." 

请帮我找出我在这里失踪。

+0

对我来说这似乎不太可能是*完整*错误。如果这是一个例外,请发布完整的堆栈跟踪。如果这是编译时错误,请发布完整的错误。 – 2015-02-06 10:33:00

+0

可以显示'downloader.LastResponse'这个'string' – 2015-02-06 10:33:53

+0

@JonSkeet这是我得到的错误“Newtonsoft.Json.Json.dll中发生类型'Newtonsoft.Json.JsonSerializationException'的未处理异常 其他信息:不能将当前JSON数组(例如[1,2,3])反序列化为类型'BookManager.lastResponse',因为该类型需要JSON对象(例如{“name”:“value”})才能正确地反序列化。 – Red 2015-02-06 10:56:25

回答

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

你需要改变什么或者是通过JSON数组中downloader.LastResponse或者您也可以按照以下步骤改变你的反序列化对象的语句。

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

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

+0

我试着改变deserialise语句,但是它给了我这个错误“”在Newtonsoft.Json.dll中发生了类型为'Newtonsoft.Json.JsonSerializationException'的未处理异常其他信息:无法反序列化当前JSON数组(例如[1,2,3])为类型'BookManager。 lastResponse',因为该类型需要一个JSON对象(例如{“name”:“value”})才能正确反序列化。“”和jason从下载器返回的响应看起来就像您在第一个块上的示例 – Red 2015-02-06 11:10:22

+0

@SthokoManqele你能分享你的JSON吗? – 2015-02-06 11:39:09

+0

{ “类型”: “书”, “元数据”:{ “BOOKID”: “书000f6b3e-f616-4417-b1b8-66f375e3e4b4”, “称号”: “互动资产审查书”, “文件名 “:” 交互式演示”, }, “当然”: “301-利布演示” ], “发行人”: “ITS”, } – Red 2015-02-06 11:57:16

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;