2016-03-08 21 views
0

我是Json与C#的新手。我试图反序列化JSON字符串以显示到数据网格中。 我成功获得来自服务器的JSON字符串,但试图反序列化时,抛出该异常:C#Json反序列化异常(“Error conversion value”id“to type'Eng_Tab.JsonData'。Path'[0]',line 1,position 5.”)

Newtonsoft.Json.JsonSerializationException:错误的转换数值 “ID”键入“Eng_Tab.JsonData”。路径'[0]',第1行,位置5. ---> System.ArgumentException:无法强制转换或从System.String 转换为Eng_Tab.JsonData。

这是数据类:

公共类JsonData { 公众诠释ID {获得;组; } public string lec {get;组; } public string sec1 {get;组; } public string sec2 {get;组; } public string sec3 {get;组; } public string sec4 {get;组; } public string sec5 {get;组; } public string sec6 {get;组; } public string sec7 {get;组; } public string sec8 {get;组; } public string sec9 {get;组; } public string sec10 {get;组; }

public int h { get; set; } 
    public int h1 { get; set; } 
    public int h2 { get; set; } 
    public int h3 { get; set; } 
    public int h4 { get; set; } 
    public int h5 { get; set; } 
    public int h7 { get; set; } 
    public int h8 { get; set; } 
    public int h9 { get; set; } 
    public int h10 { get; set; } 

    public int m { get; set; } 
    public int m1 { get; set; } 
    public int m2 { get; set; } 
    public int m3 { get; set; } 
    public int m4 { get; set; } 
    public int m5 { get; set; } 
    public int m6 { get; set; } 
    public int m7 { get; set; } 
    public int m8 { get; set; } 
    public int m9 { get; set; } 
    public int m10 { get; set; } 

} 

这里是JSON字符串:

[ “ID” 为 “1”, “H”: “7”, “M”: “0”, “LEC” :“”,“h1”:“0”,“m1”:“0”,“sec1”:“”,“h2”:“10”,“m2”:“0”,“sec2”:“Abdelrahman Mohamed401119343000“”h3“:”10“,”m3“:”0“,”sec3“:”Abdelrahman Mohamed401119343000“,”h4“:”5“,”m4“:”0“,”sec4“ A401119343000" , “H5”: “5”, “M5”: “0”, “sec5”: “A401119343000”, “H6”: “5”, “M6”: “0”, “sec6”: “A401119343000” “H7”: “5”, “M7”: “0”, “sec7”: “A401119343000”, “H8”: “5”, “M8”: “0”, “sec8”: “A401119343000”,” h9“:”18“,”m9“:”0“,”sec9“:”Abdelrahman Mohamed401119343000" , “H10”: “0”, “M10”: “0”, “SEC10”: “设定秒”]

,这是我的C#代码:

串QRY =“de = e1 & id = 1”;

WebRequest request = WebRequest.Create(“php link”+ qry);

WebResponse response = request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(),Encoding.ASCII);

string jsonData = reader.ReadToEnd(); jsonData = jsonData.Replace(“{”,“[”).Replace(“}”,“]”);}};}}};

MessageBox.Show(jsonData);

列表<JsonData> result = JsonConvert。DeserializeObject <List<JsonData>>(jsonData);

metroGrid1.DataSource = result;

回答

0

您需要在类型JsonData的对象中使用Json字符串进行反序列化。因此,你的JSON字符串应该看起来像[{"ABC":"PQR", ...}]

+0

这很好,非常感谢 –